elithin
elithin

Reputation: 191

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;
}});

Upvotes: 3

Views: 6307

Answers (4)

Raghavendra Prasad
Raghavendra Prasad

Reputation: 1

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

Example:

window.open('https://googole.com', 'Google webiste');

Upvotes: 0

roccolocko
roccolocko

Reputation: 594

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.

Upvotes: 3

Himanshu
Himanshu

Reputation: 11

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

Upvotes: 0

gdoron
gdoron

Reputation: 150253

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.

Upvotes: 6

Related Questions