Do you ever need to specify "javascript:" in onclick?

AFAIK, you do not need to specify the protocol in onclick:

onclick="javascript:myFunction()" Bad

onclick="myFunction()" Good

Today I noticed in this article on Google Anallytics that they use it:

 <a href="http://www.example.com" onClick="javascript: pageTracker._trackPageview('/outgoing/example.com');"> 

Is this example just wrong or is there a reason to specify javascript: in anything other than href ?

+43
javascript google-api
Dec 16 '08 at 18:11
source share
8 answers

Some answers here claim that the prefix "javascript:" is "left over from the old days", implying that it is intentionally specially handled by browsers for backward compatibility. Is there any solid evidence that this is so (did anyone check the source code)?

 <span onclick="javascript:alert(42)">Test</span> 

For me, it just reads like:

 javascript: alert(42); 

The meaning is that "javascript:" is just a tag and has no effect. This also works:

 <span onclick="foobar:alert(42)">Test</span> 

Update:

I experimented a bit, and it turns out that yes, "javascript:" is specially processed by IE, but definitely not like that - Firefox, Safari, Opera or Chrome:

 <span onclick="javascript:while (true) { alert('once'); break javascript; }">Test</span> 

In non-IE, this will simply warn "once" once, and then exit the loop. In IE, I get a "Shortcut not found" error. Works fine in all browsers:

 <span onclick="foo:while (true) { alert('once'); break foo; }">Test</span> 

Update 2:

I just understood the link http://crisp.tweakblogs.net/blog/the-useless-javascript-pseudo-protocol.html in one of the answers above, says a lot about the same.

+43
Dec 16 '08 at 18:53
source share

He never needed anchors and was never good practice. Anchor is for navigation only. Article on this topic Useless JavaScript: pseudo-protocol .

+7
Dec 16 '08 at 18:22
source share

At the beginning, you can also use VBScript in Internet Explorer instead of JavaScript, so specifying "javascript: ..." was standard.

Today, well, that won’t hurt ... In the future, there may always be some other wannabe-scripting language.

+5
Dec 16 '08 at 18:22
source share

See Specifying a scripting language (in 18.2.2 in the HTML 4.01 Specification, Scripts).

+5
Dec 16 '08 at 18:28
source share

I always thought this was a bad use, based on the fact that you can call JavaScript in a URL with a javascript: prefix javascript:

 <a href="javascript:void(alert('really bad usage!'))"> 

( Web forms , someone?)

And only ignorant web developers who never understood the difference between an event declaration and an href declaration used it.

I would say that even event attributes are in most cases bad practice at the moment, and the preferred way to attack the event is to use .attachEvent (Internet Explorer) and addEventListener (other browsers, as usual).

And finally ... Google is not always GOD omnipotent. They are generally more concerned that the material is working, rather than monitoring standards all the time.

+5
Dec 16 '08 at 18:43
source share

I think the prefix "javascript:" remained the same when there was still a vague possibility that something other than JavaScript could handle the event.

Today it is optional and supported for backward compatibility. But I would not say it is bad as such, it just is not necessary.

+2
Dec 16 '08 at 18:20
source share

In Internet Explorer, you can set the default language for VBScript for a page. In the early days, there was always the idea that another language could be used for scripting in a browser. As it turned out, such a language was not materialized in significant form.

I don't care about this language prefix.

+2
Dec 16 '08 at 18:22
source share

This is good practice for your maintenance programmer. The compiler knows the difference, but this young, simply extracurricular web developer cannot.

0
Dec 16 '08 at 18:16
source share



All Articles