Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window.open is opening 2 equal windows

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;
}});
like image 543
elithin Avatar asked Jan 31 '26 19:01

elithin


1 Answers

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.

like image 159
gdoron is supporting Monica Avatar answered Feb 03 '26 07:02

gdoron is supporting Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!