83

I stumbled upon something strange that I never really seen before:

javascript:a=a+10;

The line above seems to be correct and evaluates happily (at least in Firefox) just like if the javascript: part never existed.

While I do understand the purpose of the old javascript:void(...) style <a href=".."/> used during the dark ages of DHTML, I just can't figure out any useful usage of this prefix in plain JavaScript code.

Does it have some special meaning?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sitifensys
  • 2,024
  • 16
  • 29
  • 8
    I can't speak for everyone, but I've **never** seen this in plain JS code. Can you give an example? (*It might very well work, but it shouldn't have any influence what so ever, so I'd love to know where you saw this*) – h2ooooooo Sep 02 '13 at 09:30
  • Where did you see that? – Reporter Sep 02 '13 at 09:30
  • 1
    Where you are writing it ? is it in anchor tag or where ? – pixelbyaj Sep 02 '13 at 09:33
  • See http://stackoverflow.com/q/6199011/218196, http://stackoverflow.com/a/18186053/218196. – Felix Kling Sep 02 '13 at 09:36
  • As I noted, it's not in hrefs but in plain old javascript file, and I found it in one file of our old company website. – sitifensys Sep 02 '13 at 09:36
  • Seems that someone just copied a javascript bookmarklet and place it as code. – Alvin Wong Sep 02 '13 at 11:55
  • 1
    It's not just `javascript:`, anything can be used as a label by adding ":" afterwards. See [here](http://jsfiddle.net/6yLA6/) small example of breaking out of a label. – Shadow The GPT Wizard Sep 03 '13 at 12:24
  • @ShadowWizard I did understand that :), thanks to the answers, and it can be any label you like `label:` `bigfoot:` `branch_the_code_here_please:`. – sitifensys Sep 03 '13 at 12:48

4 Answers4

97

The "javascript:" is a label. It's supposed to be used to identify a loop so that you could then use "break javascript;" to break out of it, but is being misused here. It's harmless, but probably not a good idea to add a label to a statement that isn't a loop.

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
Jules
  • 14,841
  • 9
  • 83
  • 130
  • It's weird how they call it label when official (as of IANA), those are kind of link prefixes are called [URI Scheme](http://en.wikipedia.org/wiki/URI_scheme#Unofficial_but_common_URI_schemes). Well it's true that it's not officially registered with IANA but this kind of naming convention should still be used. – Marko Gresak Sep 02 '13 at 11:26
  • 17
    Except that as the question indicates, it isn't in a URI, but in plain code. As Quentin suggests below, it's almost certainly caused by somebody copy & pasting without understanding what they're doing. – Jules Sep 02 '13 at 11:43
  • Yes but it's still used as [URL](http://en.wikipedia.org/wiki/Uniform_resource_locator), even if used inside href, onclick etc. Yes I've noticed that but I got used to this kind of questions here. – Marko Gresak Sep 02 '13 at 12:09
  • 31
    This kind of language construct has been called a *label* at least 3 decades before the web was even invented. And it has absolutely nothing whatsoever to do with the web. And it has nothing to do with URIs. Why, then, should it use that terminology? – Jörg W Mittag Sep 02 '13 at 12:29
  • 1
    @JörgWMittag I find it silly that Javascript did implement *label* s (which is concidered bad by the *Knights of the Software Engineering Moral Brotherhood*) but didn't with *goto*. – sitifensys Sep 03 '13 at 12:54
  • @JörgWMittag: Perhaps, but the interpreter was written under enormous time pressure in just 10 days. http://www.computerworld.com.au/article/255293/a-z_programming_languages_javascript/?fp=4194304&fpid=1 I would argue that labels and goto are not inherently bad, but they do allow people to write very bad code http://stackoverflow.com/questions/662577/what-is-the-use-of-labels-in-c – Eric J. Sep 03 '13 at 21:39
  • 1
    The use of labels allows for labelled “break,” which I think the KotSEMB are still cool with. `OUTER: while (1) { INNER: while (1) { break OUTER; }}` – BRPocock Sep 04 '13 at 15:09
52

It is syntactically valid (it is a label) but useless. It is cargo culting caused by people copy/pasting code without understanding it.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 8
    +1 for the cargo culting link :). Copy/pasting people seems to be widespread in company staffs responsible of internal webapp design. – sitifensys Sep 02 '13 at 09:38
  • 10
    Wouldn't this wiki article on cargo cult in programming be more on the spot? [1] http://en.wikipedia.org/wiki/Cargo_cult_programming – AnyOneElse Sep 02 '13 at 11:16
12

JavaScript can also be used out of web pages in an HTML Application (HTA). In an HTA, it is possible to use a mix of VBScript and JavaScript. When you use scripting in your application, like in the following, the scripting language is automatically set to VBScript.

<SCRIPT LANGUAGE='VBScript'> MsgBox 'Hi!'</SCRIPT>

So an element with a JavaScript onclick event, like in the following, will result in an error.

<a id="myLink" href="#" onclick="MyFunction();return false;">Click me!</a>

You can solve this by explicitly set the language to JavaScript by

<a id="myLink" href="#" onclick="javascript:alert('Javascript Executed!');return false;">Click me for Javascript!</a>

Or in VBScript by

<a id="myLink" href="#" onclick='vbscript:msgbox "VBScript Executed!"'>Click me for VBScript!</a>

Note: I am aware this is a corner case, but it is an actual usage of the javascript: label (can we still call it a label in this context?) that I encountered while creating mixed language HTAs.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • 2
    A little off topic, as the original question is about using the label in plain javascript code, but It's nevertheless interesting to see how weired Internet Explorer can be ;). – sitifensys Sep 02 '13 at 10:37
-2

I agree about the uselessness of it as a label, but in some cases it is still useful. For example, you need to execute a short snippet from the address bar or write a bookmarklet. But in this case, javascript: will be more like a pseudo-protocol scheme.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
avsej
  • 3,822
  • 4
  • 26
  • 31
  • 7
    That's actually completely different. `javascript:` at the beginning of a URL makes the browser execute the rest of the URL as JavaScript. `javascript:` at the beginning of a line *of JavaScript code* is a label named "`javascript`", just like `foobar:` is a label named "`foobar`". They are entirely unrelated and occur in different contexts, but happen to look similar. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label – Peeja Sep 02 '13 at 15:54