Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the JavaScript pseudo protocol actually do? [duplicate]

Tags:

javascript

In other words what's the difference between

onclick="myFunction()"

and

onclick="JavaScript:myFunction()"
like image 971
Hoa Avatar asked Apr 09 '12 04:04

Hoa


People also ask

What is JavaScript pseudo protocol?

Another way that JavaScript code can be included on the client side is in a URL following the javascript: pseudo-protocol specifier. This special protocol type specifies that the body of the URL is arbitrary JavaScript code to be interpreted by the JavaScript interpreter.

What is pseudo protocol?

pseudoprotocol (plural pseudoprotocols) (computing) Something that can be accessed in the manner of a protocol but is not in fact a protocol. quotations ▼


2 Answers

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> 
like image 185
mplungjan Avatar answered Oct 22 '22 01:10

mplungjan


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

like image 39
Abdullah Jibaly Avatar answered Oct 22 '22 00:10

Abdullah Jibaly