javascript阻止鼠标中键从新标签页中打开链接

时间:2022-12-09 20:30:42

On some websites, you can right-click on a link and chose "open in a new tab" and it works fine, but not if one uses the middle mouse button to do so. I encountered this a few times, it's it not too annoying but I'm still curious what causes this behaviour. (About the HOW) Here is a site that behaves this way browsing with Chrome 46: http://ebookfriendly.com/free-public-domain-books-sources/ the html link tags looks normal:

在某些网站上,您可以右键单击某个链接,然后选择“在新选项卡中打开”,它可以正常工作,但如果使用鼠标中键的话,则无法正常工作。我遇到过这几次,它不是太烦人但我仍然好奇是什么导致了这种行为。 (关于如何)这是一个用Chrome 46浏览的网站:http://ebookfriendly.com/free-public-domain-books-sources/ html链接标签看起来很正常:

<a title="Feedbooks" href="http://www.feedbooks.com/">⇢ Feedbooks</a>

The cause must be something in the javascript. Any pointers?

原因必须是javascript中的内容。有什么指针吗?

3 个解决方案

#1


1  

Seems like this link has an event listener that uses preventDefault() and opens the page by other means.

看起来这个链接有一个使用preventDefault()的事件监听器,并通过其他方式打开页面。

Edit: hard to say why exactly they do this but when I look at the whole handler it seems that the link is being passed to google analytics:

编辑:很难说为什么他们这样做,但当我看到整个处理程序时,似乎链接正在传递给谷歌分析:

function(e) {
    var n = this.getAttribute("href"),
        i = "string" == typeof this.getAttribute("target") ? this.getAttribute("target") : "";
    ga("send", "event", "outbound", "click", n, {
        hitCallback: t(n, i)
    }, {
        nonInteraction: 1
    }), e.preventDefault()
}

#2


1  

You can ask which button caused the event and prevent the default behavior.

您可以询问导致事件的按钮并阻止默认行为。

document.querySelector("a").addEventListener("click", function(e) { 
   if (e.which === 2) {
      e.preventDefault();
   } 
}, false);

#3


0  

    $(document).mousedown(function(e){
        if(e.which == 2 ){
            e.preventDefault();
            alert("middle click");   
            return false;
        }
    });

works only if you keep the alert()

仅在您保持警报()时才有效

#1


1  

Seems like this link has an event listener that uses preventDefault() and opens the page by other means.

看起来这个链接有一个使用preventDefault()的事件监听器,并通过其他方式打开页面。

Edit: hard to say why exactly they do this but when I look at the whole handler it seems that the link is being passed to google analytics:

编辑:很难说为什么他们这样做,但当我看到整个处理程序时,似乎链接正在传递给谷歌分析:

function(e) {
    var n = this.getAttribute("href"),
        i = "string" == typeof this.getAttribute("target") ? this.getAttribute("target") : "";
    ga("send", "event", "outbound", "click", n, {
        hitCallback: t(n, i)
    }, {
        nonInteraction: 1
    }), e.preventDefault()
}

#2


1  

You can ask which button caused the event and prevent the default behavior.

您可以询问导致事件的按钮并阻止默认行为。

document.querySelector("a").addEventListener("click", function(e) { 
   if (e.which === 2) {
      e.preventDefault();
   } 
}, false);

#3


0  

    $(document).mousedown(function(e){
        if(e.which == 2 ){
            e.preventDefault();
            alert("middle click");   
            return false;
        }
    });

works only if you keep the alert()

仅在您保持警报()时才有效