最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - new popup window on every click - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 9

Passing 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 loaded
  • name - 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++;
发布评论

评论列表(0)

  1. 暂无评论