[英]Why is javascript:void(0) considered harmful?
任何人都可以告诉我或指向一些资源,解释为什么在超链接中使用javascript:void(0)
是有害的(特别是在Internet Explorer 6中)?
Use of the javascript:
keyword in a link isn't recommended anyway. 无论如何,不建议在链接中使用javascript:
keyword。 I've only managed to find one article on why it might be harmful: 我只设法找到一篇关于它可能有害的文章:
a href=”javascript:void(0);” — avoid the void a href =“javascript:void(0);” - 避免空白
But the general consensus shows that you shouldn't use it because it might confuse browsers without javascript support, for some of those browsers it could be parsed as an invalid link. 但普遍的共识表明你不应该使用它,因为它可能会混淆没有javascript支持的浏览器,对于某些浏览器,它可能被解析为无效链接。
Instead, you should provide a link to a page either working around the functionality that would be provided by javascript or displaying a message about the site requiring javascript to work correctly. 相反,您应该提供指向页面的链接,以解决由javascript提供的功能或显示有关需要javascript正常工作的站点的消息。 On the same link, return false;
在同一个链接上, return false;
from your event, like so: 来自你的活动,如下:
<a href="noscript.html" onclick="doSomething(); return false;">I'm a link</a>
Or alternatively, use return false;
或者,使用return false;
or preventDefault()
and returnValue
in your javascript code: 或者在你的javascript代码中使用preventDefault()
和returnValue
:
element.onclick = function ()
{
/*
// return false is better for most situations (see bobince's comment)
if (event.preventDefault)
event.preventDefault();
else
event.returnValue = false;
*/
doSomething();
return false;
}
点击<a href="javascript:void(0)" />
触发事件“beforeunload”在对象“窗口”中IE(我已在IE10中测试过),但点击<a href="#" />
doesn'吨。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.