Forum Moderators: open
Could anyone explani why we must do this somtimes? No dumb short explnation like "That's how it is." I really want an complete and accurate answer, if better a link to some big explanation.
Thanks,
Smumdax
<script type="text/javascript">
function doStuff {...}
document.write("<script src='import.js'></script>");
function doOtherstuff {...}
</script>
Some "helpful" browsers interpret the inner </script> (i.e. inside the document.write) as closing the actual <script>-- thus, doOtherstuff would never get processed. To get around this, the inner </script> is split up into two pieces-- "</" and "script>"-- so the browser never "sees" </script> until it needs to.
I haven't had to do this in a long time, so I don't even remember which browsers had this problem. If I had to guess it was probably to deal with one of the 4th generation IEs for Macintosh.
complete explanation
Well, it's just part of the whole HTML standard. A script section is, by definition, started by:
<script type="whatever">
and ended by:
</script>
So, the browser will simply look for the first occurance of </script> and assume that's where the script ends.
The same goes for everything. For example:
<!-- This is a comment...
Does it end here... -->
...or here? -->
Of course the comment is going to end after the first -->, 'cause that's what the definition of comments says.
What about this, then:
<textarea></textarea></textarea>
Not that it's valid HTML (you can't put other tags inside the text area element... but I'm doing it anyway, just to demonstrate)... But it seems obvious that the first </textarea> tag marks the end of the textarea element ('causing an invalid closing </textarea>)
That brings us back to:
<script>
document.write("</script>");
</script>
Oh, but you may say that the browser is supposed to be smart enough to realize that the tag is in the middle of a quotation.
Hmm, then what about this?
<p>The widget is 43" long.</p>
Since the double quote is not replaced by " (like it should be), is the </p> tag supposed to be "inside a quotation"? :)
I could go on and on, but I think this explains it fairly well...