简体   繁体   中英

window.open() inside multiple if conditions doesn't work in javascript

My javascript code is like this:

function openWindows() {

    if(A is true) {
      window.open(URL_A);
    }

    if(B is true) {
      window.open(URL_B);
    }

    and so on...

}

When my function is called, with all conditions true, random no. of windows open. Some times, all of them open but it happens randomly. What could be the issue? Am I losing the parent window reference? Why is this happening randomly?

Works fine for me.

Are you sure your popup blocker isn't stopping them? Many browsers don't allow you to trigger new windows without a click event or the browser will stop them. Also, the browser usually allows one window per click event.

if(true) {
    window.open('http://www.google.com');
}

if(true) {
      window.open('http://www.google.com');
}

if(true) {
    window.open('http://www.google.com');
}

if(true) {
      window.open('http://www.google.com');
}
​

http://jsfiddle.net/2LJtv/ ( warning be prepared for 4 windows to open )

is true and and so on... is not legitimate JavaScript code.


When my function is called, with all conditions true, random no. of windows open. Some times, all of them open but it happens randomly. What could be the issue?

Cannot answer w/o more information.

Am I losing the parent window reference? Why is this happening randomly?

Cannot answer w/o more information.


Your question is interesting, but the pseudocode should work, provided the following evaluate to valid values:

  • A is true
  • B is true

And the following are valid variables that contain valid values:

  • URL_A
  • URL_B

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