/    Sign up×
Community /Pin to ProfileBookmark

Bizarre syntax phenomenon

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>[/CODE]

…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>[/CODE]

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>[/CODE]

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!

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@NatdripAug 22.2011 — use eval();
Copy linkTweet thisAlerts:
@JMRKERAug 22.2011 — My guess is that the variable URL for 'drawLibrary' has not been defined

by the time you get to the document.write command.

You can only use the document.write up until the time the page has been loaded.

Writing after that, the page gets reset and all variables are reset as well.

Post some more of your code or a link to the problem.

There may still be a way around this as a problem.
Copy linkTweet thisAlerts:
@CoffeeAdventureauthorAug 22.2011 — Sorry, using eval() makes no difference.

Also, if I replace the +variable+ with the actual reference it functions exactly the same way (and doesn't work if the first form is used.) So forget about the fact that a variable is being used. The question remains: why does JavaScript parse the 2nd form and react correctly, when the syntax doesn't seem to be correct at all?

My guess is that it has to do with escape characters, and somehow I'm using a poorly documented (or undocumented) escape that is causing the behavior I've described.
Copy linkTweet thisAlerts:
@JMRKERAug 23.2011 — One too many[COLOR="Red"] ' [/COLOR]characters
<i>
</i>&lt;script type="text/JavaScript"&gt;
document.write('&lt;script type="text/JavaScript" src="'+drawLibrary+'"/&gt;[COLOR="Red"]')[/COLOR]&lt;/script&gt;');
&lt;/script&gt;

Change to:
<i>
</i>&lt;script type="text/JavaScript"&gt;
document.write('&lt;script type="text/JavaScript" src="'+drawLibrary+'"/&gt;&lt;/script&gt;');
&lt;/script&gt;
Copy linkTweet thisAlerts:
@CoffeeAdventureauthorAug 23.2011 — Thanks for catching that, but it also doesn't solve the problem.

But I've found the solution. The parser will mistake the first </script> that it encounters as closing the initial <script type=...>, so you CAN'T put a closing </script> tag inside the document.write()! This is because the document.write is INSIDE a <script> element, and the HTML parser is apparently looking for the first </script> it encounters to close the initial <script>.

Solution: FOLLOW document.write() with two sequential </script>'s; the first will be interpreted as applying to the <script> realized from within the document.write(), and the second will be interpreted as closing off the first <script>.

So here's the edited version that works:


[CODE]<script type="text/JavaScript">
document.write('<script type="text/JavaScript" src="'+drawLibrary+'"/>');</script>
</script>[/CODE]


(If you have an alternative explanation, I'm all ears!) ?
Copy linkTweet thisAlerts:
@JMRKERAug 23.2011 — Not an alternative explanation, but possibly an alternate solution.

See: http://javascript.internet.com/snippets/a-modular-toolbox.html

Allows you to add external JS after page has loaded without your special document.write scripting.
Copy linkTweet thisAlerts:
@DracoMerestAug 23.2011 — An old trick I recall using for something similar.

&lt;script type="text/JavaScript"&gt;
document.write('&lt;script type="text/JavaScript" src="'+drawLibrary+'"/&gt;&lt;/sc'+'ript&gt;');
&lt;/script&gt;
Copy linkTweet thisAlerts:
@Jeff_MottAug 23.2011 — This works too. Just a simple backslash.

&lt;script type="text/JavaScript"&gt;
document.write('&lt;script type="text/JavaScript" src="'+drawLibrary+'"&gt;&lt;[size=4][color=red][b][/b][/color][/size]/script&gt;');
&lt;/script&gt;
Copy linkTweet thisAlerts:
@CoffeeAdventureauthorAug 23.2011 — Thanks, Jeff--I like your solution best of all. It's still odd that you don't need the escape "" the first time the "/" is encountered in the text.
Copy linkTweet thisAlerts:
@Jeff_MottAug 23.2011 — Well that's because it isn't the slash that's the problem. This is just an easy way to fool the HTML parser by making </script> not look like </script>.
Copy linkTweet thisAlerts:
@zoltankisAug 23.2011 — I use it since 2008, and works fine:
[CODE]<script type="text/javascript" language="JavaScript" ><!--
// String.fromCharCode(83,67,82,73,80,84) == "SCRIPT" to fool the HTML parser
document.write('<'+String.fromCharCode(83,67,82,73,80,84)+
' TYPE="text/javascript" LANGUAGE="JavaScript" SRC="' + JS_filename_var +
'"></' + String.fromCharCode(83,67,82,73,80,84) +'>'); document.close();
//--></script>[/CODE]
×

Success!

Help @CoffeeAdventure spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...