What does the JavaScript pseudo protocol actually do? [duplicate]
The JavaScript:
TYPE/LABEL/PREFIX (could not find the actual name for it) in the event handler serves one purpose only:
In IE, IFF the FIRST script on the page is NOT JavaScript, inline JavaScript on the rest of the page had (still has?) to have javascript:
prefixing it.
It is not to be confused with the javascript:
protocol in the href (which by the way also should be avoided). href="javascript:..."
is only ever needed in old netscapes in the AREA tag. When you see the href="javascript:void(0)"
someone needs to use onclick="....; return false"
instead unless they put it there to alert the user that the link is a javascript driven one. It will fail if JS is turned off.
I looked for the official documentation from msdn, but here are discussions to back me up:
Calling VBScript from Javascript
Internet Explorer defaults to the language of the first script element it parses. So if the first script element is javascript, you shouldn't need to specify "javascript:" in your event handler.
http://www.webdeveloper.com/forum/archive/index.php/t-135462.html
You have to tell IE you are using VBS AND JScript, otherwise the assumption is all functions are VBS in this instance. Either add a (empty?) JavaScript script element [at the top of your page] or use the jscript: protocol on the onchange. onchange="jscript:location.hash=this[this.selectedIndex].value;"
Example
<html>
<head>
<script language="VBScript">
' some vbscript here forces the default language
' of the page to be VBScript and not jScript/JavaScript
</script>
</head>
<body onload="javascript:alert('I am inline in an event handler - boo me')">
.
.
<a href="..." onclick="javascript:alert('and so am I'); return false">Click</a>
.
<a href="javascript:alert('javascript: PROTOCOL is NOT the same (but avoid it too)')">
Click
</a>
</body>
</html>
As @ephemient mentions in his comment, javascript
in onclick
does nothing, it's basically a useless label. The prefix javascript
is used in the <a>
tag to tell the browser to run the following as javascript, just as if you had entered the same thing into your browser. You can try it in the location bar of your own browser and see, just enter javascript: alert("Hello")
.
To summarize:
In onclick
: the browser expects this to be javascript, so if you enter javascript:
as a prefix the browser will say: 'Oh how quaint, you put a label'.
In <a href
or in the browser location bar: the browser does not expect this to be javascript, so if you enter javascript:
as a prefix the browser will say: 'Oh, I need to run this as javascript'.