Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the label "javascript:" cause any problems?

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?

like image 694
James Allardice Avatar asked Apr 12 '13 08:04

James Allardice


People also ask

What does label do in JavaScript?

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.

What does the label element do?

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.

How do you label something in JavaScript?

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.

What do you mean by labeling?

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.


1 Answers

I'm mostly guessing here, but consider that:

  • javascript: is not an actual protocol;
  • it's not needed anywhere except bookmarklets (otherwise it's assumed to be a label);
  • the warning says it "looks like a javascript url".

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.

like image 119
bfavaretto Avatar answered Oct 26 '22 20:10

bfavaretto