3

Possible Duplicate:
What is “javascript:” in a javascript event handler?

I see a lot of code like the following:

<span onclick="javascript:alert('blah');">blah</span>

I doubt people understand what it's actually doing and I guess since it is required (as a URI scheme) in the following, people just assume it is required elsewhere?

<a href="javascript:alert('blah');">blah</a>

I.e. presumably they assume it is specifying the language of the event handler, but you can't actually specify vbscript: (or any other language) at that point.

It isn't really a problem to include the javascript:, but it does nothing as it is actually just labeling the block (as the target for a non-existent break or continue statement).

Is there a historical reason why people add the javascript:? Are there some browsers where it makes/made a difference or is it just a misunderstanding?

Community
  • 1
  • 1
Tom
  • 42,844
  • 35
  • 95
  • 101
  • 1
    *related* [What is “javascript:” in a javascript event handler?](http://stackoverflow.com/questions/6199011/what-is-javascript-in-a-javascript-event-handler), especially this comment by Andy E: *"I don't have any evidence either, but I remember seeing a piece of code that utilized features of VBScript not available to J[ava]Script, and that used `javascript:` and `vbscript:` in events. I don't think it works though, IIRC IE just picks a default to use in event attributes."*. – Felix Kling Oct 09 '11 at 23:59
  • Yes, that other question and answer covers it nicely, thanks. – Tom Oct 10 '11 at 00:14

3 Answers3

4

This creates a labeled statement.
Even though Javascript doesn't have gotos, it still has labels.

People do it because they don't understand URLs and/or Javascript, or because they're copy/pasting.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
4

Most have most likely seen some example somewhere, and follow that. The original source is of course the misunderstanding from copying the code from a href attribute, not understanding the difference between that and an event attribute.

There has never been any browser that required javascript: in an event attribute, so it's only a misunderstanding.

The reason that something like this survives and resurfaces over and over again, is that it doesn't cause any error. As it works, people think that it is correct, and repeat it in examples for others.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

It's just clearer when you want to edit something a while later.

Gilles Lesire
  • 1,237
  • 17
  • 33