0

I'm trying to access some webpage multiple times but each time I'll use different parameters.

Here's an example of what I'm trying:

var codes = [1, 2, 3, 4, 5]

for(i = 0; i <= codes.lenght; i++){
    window.open('http://localhost/applicationame/execute/cod/'+codes[i],'_self');
}

(I removed the url real names because of work related privacy)

But when I execute it in the browser console the browser only access the page containing the last element of my array. I tired using setTimeout to make the browser wait and then visit the next page but it just returns me the same behavior. I tried googling some solutions but found no good answers that apply to my case. I'm open to use JQuery too if needed or other tools that can help me with it.

2
  • 1
    If you are using chrome, then I am pretty sure that it doesn't allow you to open multiple popups at once. stackoverflow.com/a/16757736/5841629 Commented Mar 20, 2018 at 21:20
  • are you trying to access them in sequence or all at once? Commented Mar 20, 2018 at 21:32

2 Answers 2

2

That's happening because you open all instances on the same window.

Try replacing '_self' with the value of i, so you'll have a unique name parameter for each window

Sign up to request clarification or add additional context in comments.

Comments

-1

You have several problems in your code:

  • _self will open the target url in the same window - so your loop is interrupted in the same moment as you load the new page.
  • codes.lenght should be code.length

I have tried to create a working example - but was not able to. Obviously the browser does not open windows in a loop. At least Chrome shows a message that windows have been blocked.

See my test here:

function openWindows() {
  var codes = [1, 2, 3, 4, 5];
  for(i = 0; i < codes.length; i++){
      var params = {
        url: 'https://edition.cnn.com/?var='+codes[i],
        i: i
      };
      var delay = i * 2000;
      setTimeout(function(params) {
        console.log(params.url);
        window.open(params.url,'_blank', 'PopUp', params.i);
      }, delay, params);
      
  }
}
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
    <h1>Click to open windows</h1>
    <button onClick="openWindows()">Start</button>
  </body>

</html>

If you want to test, please use Plunker as playground, as SO playground does not open new windows: https://plnkr.co/edit/L318Q564spTqMVkqQUPa?p=preview

In modern web development it's rarely seen that browser windows are opened. You could try to add new DIV layers with iFrames instead.

3 Comments

You are required to post your code here, within your answer, not a third party site. Links go dead.
@Rob You are right in general - however the SO snippet behaves differently for some reason... it does not perform a "window.open". - Plunker does!
It doesn't matter. Your code must be within your question. You don't have to use the SO snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.