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 inconsole.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
4 Answers
Reset to default 6What can cause this:
- You subscribed twice.
- You're clicking twice.
- 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');