2013-04-22 19 views
0

看看在使用Shift鍵,防止瀏覽器:http://jsfiddle.net/casaschi/qKD9q/的javascript:這個例子用window.open

<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

這不能被覆蓋。大多數允許用戶在何處打開新窗口/選項卡的瀏覽器功能都不能被覆蓋。

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

試試這個。這將始終在新標籤頁中打開。即使按下Shift鍵