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.
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. ;-)
Oops, there was “<script>” inside the document.write()s above, but it got censored. ;-)
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
document.createElement(‘script’) doesn’t work in KHTML?
Well, according to http://slayeroffice.com/archives/?p=172 there are things to take care of but basically it works in Safari.
Here’s what I do: http://en.design-noir.de/webdev/JS/loadScript/. Should be fine then.
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.
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.
Dao : I’m sorry, i didn’t try very hardly asit is possible to source external .js in Safari (and KHTML ???) http://slayeroffice.com/archives/?p=172
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.
Alan, <script> inside of <body> is inappropriate. Appending to <head> is the way to go.
I’ve already posted it yesterday, but somehow it doesn’t show up: http://en.design-noir.de/webdev/JS/loadScript/ — Should work in KHTML, too (meaning Konqueror and Safari, Da Scritch).
Ah, I’m sorry. I just had a really stupid moment. Your completely right about that. s/body/head on my post.
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.