48

AFAIK, you never need to specify the protocol in an onclick:

onclick="javascript:myFunction()" Bad

onclick="myFunction()" Good

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

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

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

Ry-
  • 218,210
  • 55
  • 464
  • 476
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176

8 Answers8

50

Some of the responses here claim that the "javascript:" prefix is a "leftover from the old days", implying that it's intentionally, specially handled by the browsers for backwards compatibility. Is there solid evidence that this is the case (has anyone checked source code)?

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

To me, this just reads as:

javascript:
    alert(42);

Meaning, that "javascript:" is just a label and has no effect. This works, too:

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

Update:

I did a little experiment and it turns out that, yes, "javascript:" is handled specially by IE, but definitely not so by Firefox, Safari, Opera or Chrome:

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

On non-IE, this will just alert "once", once and then break out of the loop. On IE, I get a "Label not found" error. The following works fine in all browsers:

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

Update 2:

I just realized the link http://crisp.tweakblogs.net/blog/the-useless-javascript-pseudo-protocol.html in one of the answers above pretty much talks about the same thing.

Ates Goral
  • 137,716
  • 26
  • 137
  • 190
  • I haven't tested this but I do believe it has specific function when used with the getURL function from within Flash in that it will try to call a Javascript function on the page rather then executing the URL. – Luke Dec 16 '08 at 21:17
  • What versions of Internet Explorer had/have this behaviour? – Peter Mortensen Sep 08 '13 at 08:55
8

It's never needed on anchors and is never good practice. An anchor is for navigation only. An article about this topic is The useless JavaScript: pseudo-protocol.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
I.devries
  • 8,747
  • 1
  • 27
  • 29
8

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

Today, well, it doesn't hurt... There could always be some other wannabe browser scripting language in the future.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
5

I have always believed that it was bad usage based on the fact that you can call JavaScript within a URL with the javascript: prefix:

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

(Web Forms, someone?)

And only ignorant web-developers that never realized the difference between an event-declaration and a href-declaration used it.

I would say that even event-attributes are bad practice in most cases nowadays, and the preferred way to atach an event is by using .attachEvent (Internet Explorer) and addEventListener (the rest of the browsers, as usual).

And finally... Google isn't always GOD almighty. They tend to be of more concern that stuff works instead of following standards all the time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jishi
  • 24,126
  • 6
  • 49
  • 75
4

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Loki
  • 29,950
  • 9
  • 48
  • 62
3

I think the "javascript:" prefix is a leftover from the olde days when there still was the vague possibility that anything other than JavaScript could be handling the event.

Today it's optional and kept for backwards compatibility reasons. But I wouldn't say it's bad as such, it's just unnecessary.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
2

In Internet Explorer, It is possible to set the default language set to VBScript for a page. In the early days there was always the idea that another language may be used for scripting in a browser. As it has turned out, no such language has materialised in substantial form.

I don't bother with this language prefix myself.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
0

It's good practice for your maintenance programmer. The compiler knows the difference, but that young, just-out-of-college web developer may not.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • 3
    If the "young, just-out-of-college web developer" may not know *that*, WTF did he went to college for? :-) – Tomalak Dec 16 '08 at 18:27