Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Google split the <script> tag in their document.write() call? [duplicate]

Tags:

javascript

Possible Duplicate:
Why split the <script> tag when writing it with document.write()?

The Google DFP mobile init code contains this:

document.write('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>');

How is this any better than doing:

document.write('<script src="' + src + '"></script>');

like image 738
zsquare Avatar asked Sep 26 '26 23:09

zsquare


1 Answers

In some cases where Javascript is processed or handled -- for example when the code is embedded into an HTML page -- the character sequences </script and <script are processed separately, before the Javascript is parsed to identify string constants and the like.

Therefore it is more robust to make sure that Javascript code one writes never contains these character sequences. It is arguably a good defensive habit to do this even for code that is not currently being processed by systems that would be broken by </script> -- because that will prevent problems in case the code is later copy-pasted into a context where </script> does produce a problem.

like image 87
hmakholm left over Monica Avatar answered Sep 28 '26 12:09

hmakholm left over Monica