简体   繁体   English

为什么javascript:void(0)被认为有害?

[英]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.

相关问题 JavaScript 的“new”关键字是否有害? - Is JavaScript's “new” keyword considered harmful? Javascript:在匿名函数中使用t_this是否有害? - Javascript: is using t_this inside anonymous functions considered harmful? / regex /(string)被认为有害吗? - /regex/(string) considered harmful? 分配认为有害的jQuery函数? - assigning jQuery functions considered harmful? 是否基于被认为有害的服务器端逻辑来回应Javascript代码? - Is echoing Javascript code condtionally based on server-side logic considered harmful? 为什么我的javascript不被认为是安全的? - why is my javascript not considered secure? 链接中的tabIndex:0被认为是一种很好的做法,以避免href =“javascript:void(0);”? - Is tabIndex: 0 in links considered a good practice in order to avoid href=“javascript: void(0);”? 为什么在JavaScript中字符或字符串会被认为是虚假的 - Why would character or string be considered falsy in javascript 为什么字符串文字在 JavaScript 中被视为原始类型? - Why is the string literal considered a primitive type in JavaScript? 为什么在此 Javascript 代码中不将“0”和“未定义”视为错误? - Why '0' and 'undefined' are not being considered as false in this Javascript code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM