Im trying to open multiple pop ups at the same time. I tried using loops, but it didnt work.I don't understand why this doesn't work. Is there a better way to do this? My code:
js:
function myFunction() {
for (var i = 0; i < 5; i++) {
window.open("", "MsgWindow", "width=400, height=200");
}
}
html:
<button onclick="myFunction()">Try</button>
Im trying to open multiple pop ups at the same time. I tried using loops, but it didnt work.I don't understand why this doesn't work. Is there a better way to do this? My code:
js:
function myFunction() {
for (var i = 0; i < 5; i++) {
window.open("", "MsgWindow", "width=400, height=200");
}
}
html:
<button onclick="myFunction()">Try</button>
Share
Improve this question
asked Mar 6, 2015 at 4:21
user1930115user1930115
1,0532 gold badges12 silver badges19 bronze badges
1 Answer
Reset to default 6The second argument to window.open()
must be unique for it to open a new window or must be set to "_blank"
.
From the MDN page for window.open()
:
var windowObjectReference = window.open(strUrl, strWindowName, [strWindowFeatures]);
If a window with the name
strWindowName
already exists, thenstrUrl
is loaded into the existing window. In this case the return value of the method is the existing window andstrWindowFeatures
is ignored. Providing an empty string forstrUrl
is a way to get a reference to an open window by its name without changing the window's location. To open a new window on every call ofwindow.open()
, use the special value"_blank"
forstrWindowName
.
Note: most browsers these days have popup blockers built in. Those popup blockers will generally allow one new window to be opened when it is the direct result of a mouse click, but they may impose limits if the code is trying to open lots of windows. This popup blocker behavior is not per some specification so it likely varies in detailed implementation from browser to browser.