javascript - Why split the <script> tag when writing it with document.write()? -
why sites (or advertisers give clients javascript code) employ technique of splitting <script> and/or </script> tags within document.write() calls?
i noticed amazon well, example:
<script type='text/javascript'> if (typeof window['jquery'] == 'undefined') document.write('<scr'+'ipt type="text/javascript" src="http://z-ecx.images-amazon.com/images/g/01/javascripts/lib/jquery/jquery-1.2.6.pack._v265113567_.js"></sc'+'ript>'); </script>
</script> has broken because otherwise end enclosing <script></script> block early. should split between < , /, because script block supposed (according sgml) terminated end-tag open (etago) sequence (i.e. </):
although style , script elements use cdata data model, these elements, cdata must handled differently user agents. markup , entities must treated raw text , passed application is. first occurrence of character sequence "
</" (end-tag open delimiter) treated terminating end of element's content. in valid documents, end tag element.
however in practice browsers end parsing cdata script block on actual </script> close-tag.
in xhtml there no such special handling script blocks, < (or &) character inside them must &escaped; in other element. browsers parsing xhtml old-school html confused. there workarounds involving cdata blocks, it's easiest avoid using these characters unescaped. better way of writing script element script works on either type of parser be:
<script type="text/javascript"> document.write('\x3cscript type="text/javascript" src="foo.js">\x3c/script>'); </script>
Comments
Post a Comment