你是否需要在onclick中指定'javascript:'? [英] Do you ever need to specify 'javascript:' in an onclick?
问题描述
onclick =javascript:myFunction()
坏
onclick =myFunction()
良好
今天我在这篇关于Google Anality的文章 正在使用它:
< a href =http://www.example.comonClick =javascript:pageTracker._trackPageview('/ outgoing / example.com );>
这个例子显然是错误的,或者是否有理由指定 javascript:在
href
以外的任何内容中?
这里的一些回复声称,javascript:前缀是过去的剩余,暗示它是故意的,特别是由浏览器为了向后兼容而处理。
< span onclick =javascript:alert是否有坚实的证据表明这种情况(有人检查过源代码)? (42)>试验< /跨度>
对我来说,这只是表示:
javascript:
alert(42);
含义,即javascript:只是一个标签,不起作用。这也适用:
< span onclick =foobar:alert(42)>测试< / span>
更新:
我做了一个小实验,事实证明,是的,javascript:是由IE专门处理的,但绝对不是Firefox,Safari,Opera或Chrome处理的:
< span onclick =javascript:while(true){alert('once'); break javascript;}>测试< / span>
在非IE浏览器中,只会提醒曾经一次,然后跳出循环。在IE浏览器中,我收到找不到标签错误。以下在所有浏览器中都可以正常工作:
< span onclick =foo:while(true){alert('once' ); break foo;}>测试< / span>
更新2:
我刚刚意识到链接 http:// crisp。 tweakblogs.net/blog/the-useless-javascript-pseudo-protocol.html 在上面的答案之一很多谈论同样的事情。
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
?
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.
这篇关于你是否需要在onclick中指定'javascript:'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!