“</scr” + “ipt>”

Everyone knows that you can’t use the literal text “</script>” inside a <script> block, because the web browser will interpret it as the end of the block. But do you understand why it was designed that way, or do you think it is just a bug that no-one has got around to fixing yet?

Raymond Chen has the lowdown.

12 thoughts on ““</scr” + “ipt>”

  1. What’s the point in document.write(“”)? For evaluating scripts generated by scripts generated by scripts generated by scripts (ouch!) there’s eval(), for linking external scripts you can use e.g. createElement(“script”) and appendChild or, once again, eval() with XMLHttpRequest.

    For me, document.write(“”) is just bad style of coding. Yes, eval() is bad, too, but not *that* bad. ;-)

  2. Marcoos : Because Safari, Konqueror and some others natsy navigators doesn’t have other ways to include javascript. That’s weird, but I don’t see others ways to have (lots of) javascripts file calls.
    – Or you use in the HTML lots of LINK to .js in your HEAD… delaying a lot your first window.onlad function () . And it’s not good as this .onload is only active after ALL elements are loade (including background images in css and iframes, that can delay a maximum for your nice effects)
    – Or you do a call in a unique .js thatis calling others .js, but using ajax call or inclusion doesn’t works, except by document.write() . Nasty.

    Excuse my French

  3. I’m not trying to be a smartass, but I don’t think this is the best writeup on this problem. His suggested “workaround”, document.write(“<SCRIPT>blahblah</SCRI”+”PT>”), isn’t even wellformed HTML (document.write(“<SCRIPT>blahblah<\/SCRIPT>”) is better). Also he does not mention the difference between the script handling in HTML vs. XHTML.

  4. This is a bug in the html specification.
    Mixing two or more languages in one file isn’t smart. Javascript in a html file should have never been there. This is the reason why.

  5. Christian: All the HTML, CSS, and Javascript related stuff I’ve ever seen on MSDN is brain damaged, badly. I guess coming from the same people who brought us __doPostback() it’s not surprising they still recommend things like document.write(). If it has to be done, the best way would be something like this:


    That also allows for the javascript file to be on another server, something that XMLHttpRequest doesn’t.

    As for Da Scritch, this method will work fine (as long as you put it after the body element starts which should anyways be the case) and it performs better than document.write because the browser doesn’t have to parse the markup, plus it works with XML and XHTML. It should work in all modern browsers as well as IE.

  6. Ah, I’m sorry. I just had a really stupid moment. Your completely right about that. s/body/head on my post.

  7. Pour quelques Javascripts de plus

    C’est bien joli de mettre des effets partout… Mais si ça doit rendre un site très lent, la java en vaut-elle le lag ? Pendant la reconception de mon site, je me suis furieusement gratté la tête pour vous.