Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this mean: "document.write('<scr'+'ipt... "?

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.

Does anybody know if there's any reason for this?

document.write("<scr"+"ipt language=javascript src=http://l.yimg.com/d/lib/bc/bc_2.0.4.js></scr"+"ipt>");
like image 984
vsync Avatar asked Sep 24 '09 21:09

vsync


2 Answers

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.)

like image 86
RichieHindle Avatar answered Sep 28 '22 00:09

RichieHindle


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.

like image 26
Eric Lippert Avatar answered Sep 28 '22 01:09

Eric Lippert