3

What is javascript: used for, and is it compliant and should I use it?

I noticed that some of my associates choose to use

javascript: 

followed by some function name or some JavaScript code, but it seems unnecessary to me.

I see this a lot in event handlers like:

onclick="javascript: somefunction451();"

Also I have trouble searching for more information on javascript: because it says that I'm trying to do cross site scripting. Especially if I stick it in quotes. Is this something that simply can't be fixed? Should I search this in a Flash only or Silverlight only browser?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
RetroCoder
  • 2,597
  • 10
  • 52
  • 81
  • 2
    possible duplicate of [onclick="javascript:func()" vs. onclick="func()"](http://stackoverflow.com/questions/10242576/onclick-javascriptfunc-vs-onclick-func) –  Jun 07 '12 at 14:34
  • am not i am: Thanks for the link. Now the only question I have is why is it so difficult to do a search on javascript: in a browser? – RetroCoder Jun 07 '12 at 14:42
  • 1
    Google strips out characters like ":" in a search. Instead try "javascript followed by a colon". – Jonathan M Jun 07 '12 at 14:45

2 Answers2

7

It's completely unnecessary in "onfoo" attribute values. In that context, it's interpreted as a label by the JavaScript parser. Thus it's not erroneous, but it's useless.

In "href" values, it has a role, but there's really no reason for JavaScript "href" values anyway.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Pointy
  • 405,095
  • 59
  • 585
  • 614
1

It's "unofficial" but "common" and "works in any modern browser" in the place of a URI scheme name per this source: http://en.wikipedia.org/wiki/URI_scheme

This would apply then to <a href="javascript:... only. It's necessary there (but there are better ways to invoke javascript when clicking on an element).

In the onClick= attribute and such, it is a label and unnecessary.

Jonathan M
  • 17,145
  • 9
  • 58
  • 91