[ACCEPTED]-When do I need to specify the JavaScript protocol?-correctness
The javascript:
pseudo-protocol on event handlers will 7 be only ignored, you don't need it, the 6 JavaScript engine will interpret javascript:
as a Label Statement.
A 5 label simply provides an identifier to a 4 statement, and lets you refer to it elsewhere 3 in your program.
IMHO, this pseudo-protocol 2 is only useful for bookmarklets...
Recommended 1 article:
As other answers have mentioned, avoid the 16 use of javascript:
href links, and it is entirely unnecessary 15 in event handler attributes. However, since 14 A
tags are sometimes still semantically correct, you 13 will need to put something in the href
attribute 12 if you want your :link
and :hover
CSS styles to be 11 applied to the element in Internet Explorer. In 10 this case, use:
<a href="#" onclick="doSomething(); return false;">Link</a>
Or
<a href="javascript://" onclick="doSomething();">Link</a>
There is one (somewhat 9 obscure) bug with the javascript protocol - in Internet 8 Explorer*, it will think you are leaving 7 the page when you click the link. If you 6 are using window.onbeforeunload, then your navigate-away message 5 will appear at this time. For this reason 4 alone, we've stopped using the javascript protocol 3 completely so we don't have this bug show 2 up because we forgot to check for it when 1 we add a navigate-away message to some page.
* I probably should have specified the version when I first wrote this. I don't remember at all, but just in case the bug is present only in a now-mostly-defunct browser like IE 6 or 7, you are probably best to test it yourself.
Don't specify it at all, ever. It's wrong 6 to do it in <a>
tags, which instead should 5 be coded like this:
<a href='#' onclick='alert("Hello")'>World</a>
It's a remnant from days 4 gone by. The only time I can think of where 3 it's used would be in the browser address 2 bar (and bookmarklet bookmarks). Keep it 1 out of your pages.
In practice, you are correct.
You need to 9 do this in any instance where something 8 other than script is expected. In theory, you 7 can stick javascript:whatever
anywhere you can use a URL, but 6 this was never supported and now is officially 5 recommended against use.
However, you really shouldn't 4 use javascript:
at all. For links, you can use the 3 onclick
attribute. What is actually happening nowadays 2 is the JavaScript engine is identifying 1 javascript:
as a label, which is why the code executes.
You should all check out http://bytes.com/topic/javascript/answers/504856-javascript-pseudo-protocol-event-handlers Especially the 13 post by "Lasse Reichstein Nielsen",because 12 most answers are here are incorrect in some 11 fashion.
Also remember that the anchor tag 10 does not require a href at all! That is
<a>hi</a>
is 9 valid xhtml. The problem using href="#"
is that 8 it may scroll to the top of the page.. it 7 is simply not needed. Lastly if you do not 6 actually want the behavior of the anchor 5 tag you should not use it. You can simulate 4 an anchor using css (cursor:pointer
) and events like mouseenter 3 and mouseleave (which is more work, but 2 does not "break" the expected 1 behavior of an anchor tag).
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.