![](/img/trans.png)
[英]Javascript window.open() more than once to add item to cart
[英]Javascript: onclick window.open happens more than once
當用戶單擊頁面上的任意位置時,我將嘗試打開新標簽頁。 我只希望這種情況發生一次,否則會使他們難以瀏覽我的網站。 這是我的代碼:
document.onclick = function() {
window.open('http://example.com');
};
我的問題就像我說的,每次用戶點擊都會發生。 我怎樣才能做到只有一次?
謝謝!
您可以簡單地保留計數!
<script type="text/javascript">
var count=0;
document.onclick=function()
{
if(count==0){
window.open('http://example.com');
}
++count;
}
</script>
只需刪除onclick
就足夠了:
document.onclick = function() {
window.open('http://example.com');
document.onclick = null;
};
演示:
document.onclick=function() { alert('only show once!'); // Unset the onclick function. document.onclick = null; }
為窗口添加名稱
window.open('http://example.com','_test');
如果可以使用jquery:
$(document).one('click', function() {
window.open('your-url')
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.