Both JSLint and JSHint issue warnings when they encounter a labelled statement whose identifier matches the following regular expression:
/^(?:javascript|jscript|ecmascript|vbscript|mocha|livescript)\s*:/i
For example, the following snippet generates a "JavaScript URL" warning from JSLint and a "Label 'javascript' looks like a javascript url" warning from JSHint (the function wrapper is unnecessary, but JSLint doesn't like labelled statements that are not function-scoped and raises a different warning):
function example(x, y) {
javascript:
while (x) {
while (y) {
break javascript;
}
}
}
As far as I can tell, no browser cares about it, even when it appears immediately after the "javascript:" protocol in a bookmarklet. For example, the following always seem to work (just paste into the address bar like any bookmarklet):
javascript:(function () { javascript:for(var i = 0; i < 2; i++) { alert(i); break javascript; } }());
javascript:javascript:for(var i = 0; i < 2; i++) { alert(i); break javascript; }
Could the label identifier "javascript:" (or any other string that would match that regex) ever have caused any issues (some ancient browser perhaps?) that would warrant the warnings generated? Why are these warnings generated?
You can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution. Note that JavaScript has no goto statement, you can only use labels with break or continue . In strict mode code, you can't use let as a label name.
A <label> is used to create a caption for a form control. The <label> can be associated with a form control either implicitly by placing the control element inside the label element, or explicitly by using the for attribute.
To label JavaScript statements, simply precede the statements with a label name and a colon, and then followed by the statements themselves. Description: label — Naming as an identifier. statement(s) — Any JavaScript statement.
Labelling or using a label is describing someone or something in a word or short phrase. For example, the label "criminal" may be used to describe someone who has broken a law. Labelling theory is a theory in sociology which ascribes labelling of people to control and identification of deviant behaviour.
I'm mostly guessing here, but consider that:
javascript:
is not an actual protocol;I think JSLint is suggesting that the javascript:
pseudo-protocol is bad, and so is anything that resembles it, or could be confused with it. Could it cause any problems? Strictly speaking, perhaps on IE (maybe oldIE only). But, as I said, a javascript:
label might be confused with the pseudo-protocol, and JSLint doesn't like anything potentially confusing. So, this might be an explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With