简体   繁体   中英

Open link in new tab when user clicks

I want to create a jQuery script that opens a specific url in a new tab if the user clicks somewhere (no matter where). After that, the user should be able to click anywhere without getting a new tab at every click again.

Unfortunately I have no idea how to create such a script, and therefore I would appreciate all your answears:)

You can use the .one() event like

$(window).one('click', function() {
 var url = "http://google.com";
 window.open(url, "_blank");
 })

_blank in window.open() is used to open the link in a new tab.

DEMO

Refer to following links for documentation on .one() and window.open()

 $(window).one('click', function() {
   var url = "http://google.com";
   window.open(url, "_blank");
 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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