In other words what's the difference between
onclick="myFunction()"
and
onclick="JavaScript:myFunction()"
In other words what's the difference between
onclick="myFunction()"
and
onclick="JavaScript:myFunction()"
The JavaScript:
TYPE/LABEL/PREFIX (could not find the actual name for it) in the event handler serves one purpose only:
In Internet Explorer (which supported VBScript as a browser language),
IFF the FIRST script on the page is NOT JavaScript, inline JavaScript on the rest of the page had 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.
Even better is to remove inline event handlers and use addEventListener - delegate when more than one element that needs the same eventListener.
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.
https://www.webdeveloper.com/forum/archive/index.php/t-135462.html
(link not working anymore)
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('The javascript: PROTOCOL is NOT the same')">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'.