I've a pop up window which i'm opening using below script. On every click, i want to open new pop up. I understand, having unique name for the window will solve the problem (In this case "SampleWindow"). What's the best way to maintain the uniqueness of the window ? Is there any other way to manage javascript popup ?
window.open(url, 'SampleWindow', 'WIDTH=300,HEIGHT=250');
I've a pop up window which i'm opening using below script. On every click, i want to open new pop up. I understand, having unique name for the window will solve the problem (In this case "SampleWindow"). What's the best way to maintain the uniqueness of the window ? Is there any other way to manage javascript popup ?
window.open(url, 'SampleWindow', 'WIDTH=300,HEIGHT=250');
Share
Improve this question
asked Feb 5, 2013 at 19:00
PankajPankaj
3,66417 gold badges52 silver badges89 bronze badges
1
- 1 Either of the below 3 answers would work. It's preference. – Joe R. Commented Feb 5, 2013 at 19:45
3 Answers
Reset to default 9Passing a value of _blank
to the name
parameter will load the passed URL
into a new window. Like this:
window.open(url, '_blank', 'width=300,height=250');
Other options for name
parameter (optional) include:
_blank
- URL is loaded into a new window. This is default_parent
- URL is loaded into the parent frame_self
- URL replaces the current page_top
- URL replaces any framesets that may be loadedname
- The name of the window
Do note that if you'll be passing this JavaScript to an anchor tag A
that IE browsers expect a return value, otherwise it'll referrence the A
tag itself. Your code for that would look like:
<a href="javascript:var w=window.open(url, '_blank', 'width=300,height=250');">test</a>
or better (to avoid showing users your code in the status bar):
<a href="javascript:void(0);" onclick="window.open(url, '_blank', 'width=300,height=250');">test</a>
Links:
- window open() method reference
- JSFiddle demo
You can append timestamp to your window name to create truly unique window names and assign that to variable. as...
var myWindow = window.open(url, 'SampleWindow'+new Date().getTime(), 'WIDTH=300,HEIGHT=250');
Since you want more than windows then it will be better to manage the windows through array.
var myWindow = [];
myWindow.push(window.open(url, 'SampleWindow'+new Date().getTime(), 'WIDTH=300,HEIGHT=250'));
These windows can be managed by myWindow[index]
like myWindow[2].close();
.
You could possibly just change the name each time
var i = 0;
window.open(url, 'SampleWindow' + i, 'WIDTH=300,HEIGHT=250');
i++;