Why /*@ isn't a comment in JavaScript?
Asked Answered
P

3

15

This script http://html5shiv.googlecode.com/svn/trunk/html5.js looks like a big comment, but it works. Why /*@ doesn't behave like a comment?

Poulterer answered 8/4, 2011 at 13:52 Comment(1)
More on SO related to this hereToratorah
L
22

This uses a proprietry Microsoft feature called conditional compilation. As far as non-Microsoft browsers are concerned, it is a comment (but they don't need the shiv).

Lynnett answered 8/4, 2011 at 13:54 Comment(2)
But if this /*@ is only for IE, why this script is included in html with another condition: <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]-->. What this double-check is needed for?Poulterer
I can only guess at the author's motivation, but conditional comments stop other browsers trying to download it, while conditional compilation stops it being executed if you roll it up into your standard single minified JS package.Lynnett
B
5

Here some more info and the source.

It is a comment - multiline comment. All browsers suports comments ( // or /* ... */ ) in JavaScript. But only IE checks for the @ in the comment and recognize the part of the script for the conditional compilation. This is for usage of the non-standart Microsoft features.

There is also conditional commentsMSDN in HTML, which will be removed in IE10.

Bargeboard answered 8/4, 2011 at 13:57 Comment(0)
V
0

It's a hack that uses IE's strange way of not considering /*@ a comment.

Varmint answered 8/4, 2011 at 13:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.