/    Sign up×
Community /Pin to ProfileBookmark

</script> in a comment?

It seems that </script> in a comment ends the <script>!

[CODE]<script type=’text/javascript’>
var x = 2*glob*3;
// var y = x*x; </script>
var y = x+x;
</script>[/CODE]

or

[CODE]<script type=’text/javascript’>
var x = 2*glob*3;
/*
var y = x*x; </script>
*/
var y = x+x;
</script>[/CODE]

Both scripts are ended before var y = x+x; is evaluated (and it appears logically on the page as it is interpreted as HTML).
It seems that </script>, even commented, ends the script!

Both IE6 and FF2 ends the script in the comment, so that must be the rule, I just find this quite weird?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledApr 29.2008 — it isn't weird, that how should work. you have to rethink your thought. before browser execute any javascript statement, it has first to render the script tag, so that any content within the script tag may be ignored, or better said, isn't executed. otherwise, how the javascript interpreter may know where end the content?
Copy linkTweet thisAlerts:
@3_1_4_1_5authorApr 29.2008 — it isn't weird, that how should work. you have to rethink your thought. before browser execute any javascript statement, it has first to render the script tag, so that any content within the script tag may be ignored, or better said, isn't executed. otherwise, how the javascript interpreter may know where end the content?[/QUOTE]
I was expecting it to switch to the given Language parse mode when it meets a <script> tag. So you'll tell me "the browser may not understand the given language" - right, and in this case it stops at the first </script> wherever it is. Otherwise it should parse (only Syntax) the text with the given language rules.

For convenience, and because those languages are "loose" this works this way but in a more formal world, it shouldn't. "The browser [i]skips[/i] everything until it meets </script>" is an implementation choice made by the browser developers. It could also interpret the <b> tag in a JS comment... but it doesn't.[CODE]HTML text
<script type='text/javascript'>
var x = 333;
// Another comment with <b> inside
</script>
This text is not bold though
[/CODE]
A funny note from W3c

[COLOR="Navy"]Note. Some browsers close comments on the first ">" character, so to hide script content from such browsers, you can transpose operands for relational and shift operators (e.g., use "y < x" rather than "x > y") or use scripting language-dependent escapes for ">".

[/COLOR]
from here.

Anyway, thank you for your reply, just needed a confirmation. Coming from C++, I'm always amazed with HTML and JS ? The JS non mandatory ending ';' is another interesting example :rolleyes:
Copy linkTweet thisAlerts:
@felgallApr 30.2008 — That's why all end tags inside of inline JavaScript must be escaped eg. </script> so that they don't get mixed up with the surrounding HTML.

Of course the better solution is to not embed JavaScript into HTML files in the first place and keep it in a separate file of its own.
×

Success!

Help @3_1_4_1_5 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...