简体   繁体   中英

How to prevent window.open function from opening a window more than once? Return function?

Making a game with JavaScript (p5.js, p5.play) in which once the user has collected all the sprite objects (stored in food array), it will show a text (in this case this emoji) and will open a window leading to another webpage. The issue here is that most browsers have pop-up blockers, ideally would like the user to click on the emoji which is attached to the link of the webpage, opening up a new window. Tried using .onclick and it still doesn't work...

What's happening right now is that when I return to the webpage of the game, it keeps popping up a new window. Is there a solution to this?

Original link to edit the code is here; https://glitch.com/edit/#!/kitschen-dreams Line 53 on sketch.js file

if (food.length > 0) {
        text(score, width / 2, height / 2);
      } else {
        var kitschenDreams = text("😍", width / 2, height / 2);
        // kitschenDreams.onclick = window.open("http://kitschendreams.tumblr.com");
        // window.open("http://kitschendreams.tumblr.com");
      }
    }

To prevent a window from opening more than one time, you can specify a window name (second parameter of Window.open() ):

A DOMString specifying the name of the browsing context (window, <iframe> or 
tab) into which to load the specified resource; if the name doesn't indicate an 
existing context, a new window is created and is given the name specified by 
windowName.

Consecutive calls with the same window name re-use the previously openend window.

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