cschol
cschol

Reputation: 13059

Why break up the string argument to `document.write(…)` into multiple literals and then concatenate them back?

I was playing around with a Python-based HTML parser and parsed Stackoverflow. The parser puked on a line with

HTMLParser.HTMLParseError: bad end tag: "</'+'scr'+'ipt>", at line 649, column 29

The error points to the following lines of JavaScript in the site's source:

<script type="text/javascript">
    document.write('<s'+'cript lang' + 'uage="jav' + 'ascript" src=" [...] ">'); 
    document.write('</'+'scr'+'ipt>');
</script>

([...] replace a long link, which is removed for simplicity)

Out of curiosity, is there a specific reason for what looks to me like artificial 'obfuscation' of the code, i.e. why use the document.write method to concatenate all the chopped up strings?

Upvotes: 0

Views: 237

Answers (4)

harto
harto

Reputation: 90483

When the HTML parser encounters document.write('</script>');, it thinks it has found the end of the enclosing <script> tag. Breaking the tag up stops the parser from recognising the closing tag.

The other way I've seen this achieved is by escaping the slash, i.e. document.write('<\/script>');.

The correct way to do this is either:

  • Enclose the body of the script in a <![CDATA[ ... ]]> block (if serving XHTML), or
  • Put the script in an external file, or
  • Use the DOM API instead (i.e. create a script node and append that to the document head)

Upvotes: 1

Gabriel McAdams
Gabriel McAdams

Reputation: 58253

Perhaps its there to stop programs that search specifically for script tags. Ad blockers, for example, look for script tags and object tags.

Upvotes: -1

avpaderno
avpaderno

Reputation: 29669

It has been written in that way to avoid the browser thinks it's the closing tag for <script>, which would cause some problems.

Upvotes: 2

Derek Illchuk
Derek Illchuk

Reputation: 5658

I think it's to fight adblockers.

... + 'uage="jav' + 'ascript" src="http://ads.stackoverflow.com

Upvotes: 7

Related Questions