What's the point of "javascript:" in code (not URLs)?

Javascript

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsitifensysView Question on Stackoverflow
Solution 1 - JavascriptJulesView Answer on Stackoverflow
Solution 2 - JavascriptQuentinView Answer on Stackoverflow
Solution 3 - JavascriptAutomatedChaosView Answer on Stackoverflow
Solution 4 - JavascriptavsejView Answer on Stackoverflow