What's the point of "javascript:" in code (not URLs)? [duplicate]
Solution 1:
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:
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 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.