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

时间:2013-04-22 11:52:47

标签: javascript

请看这个例子: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?

如果单击chrome或IE上的蓝线,将打开一个新选项卡。 如果您按住Shift键单击蓝线,则会打开一个弹出窗口。

有没有办法控制我的javascript代码如何打开新窗口,例如总是作为一个新窗口,无论用户是否按下shiftKey?

感谢。

2 个答案:

答案 0 :(得分:2)

无法覆盖。大多数浏览器功能都无法覆盖用户在打开新窗口/选项卡的位置的首选项。

答案 1 :(得分:1)

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

试试这个。这将始终在新选项卡中打开。即使按下shift键