I\'ve seen this on every Yahoo! news page, at the bottom of the source code,
and failed to understand why they break the script word like that.
<
so that it doesn't get evaluated but gets inserted as a string.
Some browsers tend to "act" to fast when parsing a document and immediately try to execute the javascript when they find a script tag (even though it is itself in a piece of js). To avoid this they break the decalration of the tag.
For a full discussion of this, see:
http://www.wwco.com/~wls/blog/2007/04/25/using-script-in-a-javascript-literal/
The short answer is that your code is parsed in two discrete steps.
The first one is XML. And that means that the element <SCRIPT> is looking for a </SCRIPT>. It's important to remember that XML elements are content agnostic. That means that the parser doesn't know yet that there's JavaScript in there.
Once it has the contents of the <SCRIPT> element, then it processes that chunk of text, which presumably is JavaScript.
By splitting up the tag with a string concatenate operator you prevent a constant from tripping up the XML phase.
One simple solution is to put < and > in the Javascript text.
It's a bad way to prevent XML/XHTML and HTML validators from yelling at the source code.
Suppose you are writing a tool that detects the beginning and end of script blocks in a chunk of text. Suppose you see
<blah><blahdeblah><script>
blah blah blah
blah
print("</script>")
print("<script>")
blah
</script>
</blahdeblah></blah>
Without knowing the syntax of the script language, how does your tool know that this is ONE script block and not TWO script blocks with ")blah between them?
A web browser is such a tool. It's a reasonable practice to make sure you never confuse the web browser by never having <script>
or </script>
in your file unless it actually is a script tag.
Consider this simplified example:
<script>
document.write("something </script> something");
</script>
The browser's HTML parser would see the </script>
within the JavaScript string and interpret that as the end of the script element.
The HTML parser doesn't know about JavaScript syntax - all it knows is that the <script>
element ends at the next </script>
.
(It also knows that you can't have nested <script>
elements, hence the breaking of the opening <script>
as well as the closing </script>
in your example.)