I’ve encountered a strange limitation and then an equally odd solution to writing inline javascript in an HTML tag.
I’m trying to write:
[CODE]<script type=”text/JavaScript” src=drawLibrary></script>
…where drawLibrary is a variable containing the URL of the draw library for the project in question. (This is going in the header of my HTML). Of course, you can’t access a JavaScript variable in HTML directly. So I tried using the document.write() routine within a <script> section:
[CODE]<script type=”text/JavaScript”>
document.write(‘<script type=”text/JavaScript” src=”‘+drawLibrary+'”></script>’);
</script>
The problem is that it doesn’t work; what happens is that this is printed to screen:
‘);
If anyone can explain why that happens, I’ll be very glad to know. But the main reason I’m writing is that during experimentation I chanced upon the following syntax, which works, inexplicably:
[CODE]<script type=”text/JavaScript”>
document.write(‘<script type=”text/JavaScript” src=”‘+drawLibrary+'”/>’)</script>’);
</script>
It may be hard to see what’s different here. Instead of following “‘+drawLibrary+'” with:
>
…I’m following it with
/>’)
This makes the code work.
But why???
On the face of it, this would seem to be incorrect syntax. But both IE and FireFox process it without any problems and without reporting any errors. Any clues? What have I accidentally chanced upon?
Many thanks in advance!