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

javascript - Window.open is opening 2 equal windows - Stack Overflow

programmeradmin1浏览0评论

I have the following html/jquery code, that is supposed to open a new page, but is opening 2:

$('.div_link').live('click', function(){
    window.open($(this).attr('url'), '_blank', 'toolbar=no,resizable=yes,location=yes,menubar=yes');
});

<div class="div_link" url="/test/as/8888888-888">8888888-888</div>

So, everything is working fine, except that I get two new windows with the exact same content in them. I've seen people suggesting that there was something to do with returning false in the onclick event, but I don't think it's the case here.

Also, I've tried to do something like:

var handler = window.open(...);

Edit: Tried something alike what gdoron suggested but then it doesn't open any window, and the click event isn't fired.

$('div.div_link').on({
    click: function(){
        window.open($(this).attr('url'), '_blank','toolbar=no,resizable=yes,location=yes,menubar=yes');
        return false;
}});

I have the following html/jquery code, that is supposed to open a new page, but is opening 2:

$('.div_link').live('click', function(){
    window.open($(this).attr('url'), '_blank', 'toolbar=no,resizable=yes,location=yes,menubar=yes');
});

<div class="div_link" url="/test/as/8888888-888">8888888-888</div>

So, everything is working fine, except that I get two new windows with the exact same content in them. I've seen people suggesting that there was something to do with returning false in the onclick event, but I don't think it's the case here.

Also, I've tried to do something like:

var handler = window.open(...);

Edit: Tried something alike what gdoron suggested but then it doesn't open any window, and the click event isn't fired.

$('div.div_link').on({
    click: function(){
        window.open($(this).attr('url'), '_blank','toolbar=no,resizable=yes,location=yes,menubar=yes');
        return false;
}});
Share Improve this question edited Jan 2, 2019 at 3:37 Cœur 38.7k26 gold badges204 silver badges277 bronze badges asked Jan 31, 2013 at 17:20 elithinelithin 1911 gold badge4 silver badges14 bronze badges 3
  • 2 Are you definite the function is not being called twice? Try adding a console.log before the window.open – Undefined Commented Jan 31, 2013 at 17:23
  • +1 for Sam, ment out window.open... and add in console.log("You've clicked this once"); – SSH This Commented Jan 31, 2013 at 17:24
  • ok, i did what you suggested, and yes, the function is being called twice. what can I do to call it just once? – elithin Commented Feb 1, 2013 at 13:08
Add a ment  | 

4 Answers 4

Reset to default 6

What can cause this:

  1. You subscribed twice.
  2. You're clicking twice.
  3. You have two nested divs with the class div_link and the event bubbles.

Regarding the last option, use on instead of the deprecated(1.7)-deleted (1.9) live function:

$('#containerId').on('click', '.div_link', function(){
    window.open($(this).attr('url'), '_blank', 'toolbar=no,resizable=yes,location=yes,menubar=yes');
    return false;
});

You can stop the bubbling in on unlike with live.

I had this exact same problem, I solved by giving the second parameter a fixed name instead of using _blank. Meaning the following:

$('.div_link').live('click', function(){
    window.open($(this).attr('url'), 'myFixedName', 'toolbar=no,resizable=yes,location=yes,menubar=yes');
});

This solution does not prevent the double click but it gives the desire behavior. I hope it helps somebody.

Remove that '_blank' at the second line. it solved my same exact issue.

Yes removing _blank and replacing it with mon Name like website title where you want to redirect

Example:

window.open('https://googole.', 'Google webiste');
发布评论

评论列表(0)

  1. 暂无评论