19

In other words what's the difference between

onclick="myFunction()"

and

onclick="JavaScript:myFunction()"
Hoa
  • 19,858
  • 28
  • 78
  • 107

2 Answers2

21

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> 
mplungjan
  • 169,008
  • 28
  • 173
  • 236
7

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'.

Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
  • It is the "just as if you had entered the same thing in your browser" I do not agree with. The javascript PREFIX/LABEL turns magically into a PROTOCOL if you paste it into the browser, but that is purely a side effect and has nothing to do with the onclick – mplungjan Apr 09 '12 at 05:53
  • Thats is exactly what I'm saying, I updated the answer to try to make it more clear because obviously I have not done a good job with that. – Abdullah Jibaly Apr 09 '12 at 06:01