What is the reason for people putting "javascript:" in onclick and other event handlers? [duplicate]
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?
This creates a labeled statement.
Even though Javascript doesn't have goto
s, it still has labels.
People do it because they don't understand URLs and/or Javascript, or because they're copy/pasting.
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.
It's just clearer when you want to edit something a while later.
精彩评论