Reputation: 13059
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
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:
<![CDATA[ ... ]]>
block (if serving XHTML), orUpvotes: 1
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
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
Reputation: 5658
I think it's to fight adblockers.
... + 'uage="jav' + 'ascript" src="http://ads.stackoverflow.com
Upvotes: 7