-10

I have a requirement where I have to open a URL in several windows with window.open in for loop.

But the next window has to open only after previous one is loaded.

I have this code:

for(i=0; i<5; i++){    
    window.open("http://myurl.com");    
}

Which opens 5 windows with 5 URLs. But not sure how to delay opening of next window until the previous one is completely loaded.

f-CJ
  • 4,235
  • 2
  • 30
  • 28
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
  • 5
    What's the reason you need to do this? It sounds exceptionally 'spammy'. Browsers will most likely block any attempt to open windows that quickly anyway. Also, you have 11k reputation, I would have hoped you know how to ask a more constructive question. – Rory McCrossan Apr 28 '15 at 09:39
  • Umm... Whut are you looking for ? And what have you attempted ? – Pogrindis Apr 28 '15 at 09:39
  • possible duplicate of [Detecting the onload event of a window opened with window.open](http://stackoverflow.com/questions/3030859/detecting-the-onload-event-of-a-window-opened-with-window-open) – Bassdrop Cumberwubwubwub Apr 28 '15 at 09:40
  • 1
    you could use `setTimeout` ? But I don't want to support your spammy malicious interests! :) – Pogrindis Apr 28 '15 at 09:45
  • @RoryMcCrossan I dont know whats wrong with this question. I have to open 5 different windows of a URL with different parameters but i just need to open a window only when the previous one has been loaded. I dont understand the negatives. – sushil bharwani Apr 28 '15 at 09:46
  • 1
    @sushilbharwani your original question was vague, no examples given and no proof of work not to mention no clear question. – Pogrindis Apr 28 '15 at 09:46
  • @Pogrindis I dont have any spammy malicious interests, We have a list of 5 items when we check them we want to see the details of these items. Which are supported by another application. – sushil bharwani Apr 28 '15 at 09:48
  • @sushilbharwani well whatever its for there is probably a better cleaner way to achieve this if you could explain a little more the background as to what you are looking to achieve! – Pogrindis Apr 28 '15 at 09:49
  • Yes i am looking for a better cleaner way that i dont know and getting all these negatives. Well the requirement says there are a list of items and one can select as many items and click on open button. On open button click all these items details open. The details of these items are maintained by another URL. So we simply have a loop which window opens these different items. But for some reason we want the next window to open only after previous one has loaded. What do you think is the right approach. – sushil bharwani Apr 28 '15 at 10:01

1 Answers1

1

Take window.open() as element

var w = window.open('http://myurl.com')

And add it a listener that triggers next url when loaded

w.addEventListener('load', yourFunction(), true);
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109