简体   繁体   English

javascript:使用window.open时,禁止浏览器使用shiftKey

[英]javascript: prevent browser using shiftKey when using window.open

Look at this example: http://jsfiddle.net/casaschi/qKD9q/ 看这个例子: http : //jsfiddle.net/casaschi/qKD9q/

<div style="color:blue;" onclick="window.open('http://www.google.com/search?q=' + Math.random(), '_blank');">
click me while pressing the shift key and then while NOT pressing it
</div>
<br/>
does one window appear as popup and the other one as a new tab?

If you click on the blue line on chrome or IE, a new tab will open. 如果单击chrome或IE上的蓝线,将打开一个新选项卡。 If you shift-click on the blue line a popup window will open instead. 如果按住Shift键并单击蓝线,则会打开一个弹出窗口。

Is there a way to control from my javascript code how the new window is opened, for example always as a new window, regardless of the user pressing shiftKey or not? 有没有一种方法可以从我的javascript代码中控制新窗口的打开方式,例如始终作为新窗口打开,而不管用户是否按下shiftKey?

Thanks. 谢谢。

That can't be overridden. 那不能被覆盖。 Most browser features that allow the user's preferences in where to open a new window/tab can't be overridden. 大多数浏览器功能(允许用户在哪里打开新窗口/选项卡的首选项)都不会被覆盖。

$(document).ready(function () {
    $('div').keypress(function(e) {
    console.log(e.shiftKey);
    if (e.which == 163) {
        e.preventDefault();
    }
    });
});

Try this. 尝试这个。 This will always open in new tab. 这将始终在新标签页中打开。 Even with shift key pressed 即使按下Shift键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM