How to open new tab only 1 time?

2019-09-08 04:12发布

问题:

<script>
$(document).click(function() { 
    window.open("http://google.com", "_blank");
});
</script>

With above code, when I click anywhere, the tab will be opened. However, when I click more, the new tab will be opened. So, how to disable above code after click 1 time ?

回答1:

Use jquery .one() http://api.jquery.com/one/

<script>
$(document).one("click",function() { window.open("http://google.com", "_blank");});
</script>