-1

I'm trying to step through an array and pass my urls stored there to the window.open function. This is what I have.

$('a.linked').click(function(e) {
    e.preventDefault();
    var i;
    var urls = ['http://example.com', 'http://example2.com', 'http://example3.com'];

    for (i = 0; i < urls.length; i++) {
      var ts = i*3500;
      setTimeout(function(){window.open(urls[i]); }, ts);
  }
});

This is opening the new windows at the correct timing but I'm getting about:blank. I thought it might be not escaping quotes.

The duplicate answer says I'm losing my value of i However, when I remove the setTimeout I get all the new windows with the correct urls. The following code demonstrates not losing the value of i.

$('a.linkedinProfiles').click(function(e) {
    e.preventDefault();
    var i;
    var urls = ['http://example.com', 'http://example2.com', 'http://example3.com'];
    for (i = 0; i < urls.length; i++) {
    var ts = i*3500;
    {window.open(urls[i]); };   
    } });

I know this has to be something simple I am missing. Thanks in advance.

Ryan Litwiller
  • 497
  • 5
  • 24

1 Answers1

0

May not be the full solution, but you could try adding "http://" to the beginning of your URLs. Currently they will be opened as relative links.

TW80000
  • 1,507
  • 1
  • 11
  • 18