Is the javascript:
prefix really needed? I know you should aim for unobtrusive JavaScript but under what circumstances would something break if it is not there?

- 53,220
- 42
- 124
- 197
-
1Possible duplicate of http://stackoverflow.com/questions/372159/do-you-ever-need-to-specify-javascript-in-an-onclick – Demian Brecht May 03 '11 at 22:28
-
@Demian: Highly related but I'm not sure that they're "exact" duplicates. – Lightness Races in Orbit May 03 '11 at 22:31
-
@Demian, thanks for that link, interesting that it may just be a simple label in an onclick/event handler definition. – Abdullah Jibaly May 03 '11 at 22:34
-
@tomalak: Yeah, wasn't sure - wanted to point the OP to pre-existing answers on a similar thread though :) – Demian Brecht May 03 '11 at 22:36
5 Answers
javascript:
is a URI scheme.
It is needed to create a URI that runs Javascript, either in an href=""
attribute or in the browser address bar.
There is never a situation in which javascript:
is optional.
Best practices indicate that javascript:
URIs should be avoided where possible in favor of click
handlers, so its use is frowned upon.
However, there are cases where there is no alternative. For example, bookmarklets can only be created by using javascript:
.

- 868,454
- 176
- 1,908
- 1,964
-
So does this mean that in the onclick attribute they are also necessary? – Flipper May 03 '11 at 23:40
-
@Flipper: **No**. The only reason it works in `onclick` is that it happens to be valid Javascript syntax (a `goto` label). `onclick` isn't a URI. http://stackoverflow.com/questions/372159/do-you-ever-need-to-specify-javascript-in-an-onclick – SLaks May 03 '11 at 23:53
They're "needed" if you are encoding Javascript code into an URI, for example in the href
property of an <a>
tag.

- 1
- 1

- 378,754
- 76
- 643
- 1,055
If you put JavaScript code into the href
attribute of an a
, or other attribute which takes a URL, then it's required for the browser to detect that it is JS. It's not necessary (and may not even work) if you use it with onclick
or other attributes that already expect JS code.

- 42,745
- 10
- 68
- 86
That "prefix" is used only in the href attribute of an html anchor (). It is never actually needed, since you could as well define a click event handler.

- 355
- 3
- 11
It is needed even in onclick situations if you also have a VBS routine on the same page.

- 11
- 1