1

Ok, I feel I have done my due diligence here... JSFIddle - http://jsfiddle.net/taytayevanson/8BpHw/5/

I am trying to create a page that will pop multiple tabs. I understand that using this code...

<a href="google.com" target="_blank">New Tab</a>

will pop 1 new tab. However, as explained in this stackoverflow q/a, it needs to be a "user initiated event" for Chrome to pop a new tab and not a window. Because of this requirement, a loop will pop 1 new tab and then new windows for each following link.

jQuery("a.site").each(function(){
    var string = jQuery(this).attr("href") + "/" + jQuery("#arguments").val();
    jQuery(this).attr("href",string);
    jQuery(this).trigger('click');
});

I have tried programmatically creating links and clicking them, with different plugins, timeouts, methods, and I even tried "daisy-chaining" the process and firing it on a page load (a huge PHP/GET variable/page load trigger thing) but it would still pop windows because the event was not user initiated.

I tried this...

function clickLink(link) {
var cancelled = false;

if (document.createEvent) {
    var event = document.createEvent("MouseEvents");
    event.initMouseEvent("click", true, true, window,
        0, 0, 0, 0, 0,
        false, false, false, false,
        0, null);
    cancelled = !link.dispatchEvent(event);
}
else if (link.fireEvent) {
    cancelled = !link.fireEvent("onclick");
}

if (!cancelled) {
    window.location = link.href;
}
}

and although I can read it, I don't understand it well enough to comprehend what i'm supposed to pass into this function. I tried something like this...

jQuery("a.site").each(function(){
    var string = jQuery(this).attr("href") + "/" + jQuery("#launcher").val();
    jQuery(this).attr("href",string);
    clickLink(jQuery(this));
});

But I get a "object has no method 'dispatchEvent'" console error. I tried using that same "var event" and just calling...

link.trigger(event);

but there was a console error there as well. The only thing I have not tried is in this Q/A (using jQuery.get() to open a tab and write to it) but seeing as it still calls window.open(), I feel like i'll still run into the exact same loop issue.


Ok. Got all that out of the way... Is there a real answer for this besides "it's controlled by your browser" ? I feel like there must be a way. Thank you, sorry for the novel :)

Community
  • 1
  • 1
Taylor Evanson
  • 384
  • 4
  • 16
  • 1
    There isn't. It's a security thing the browser does, to keep bad actors from popping up unwanted pages for users. Why on earth are you wanting to do this anyhow? Maybe you could rethink your approach to the problem? – jameslafferty Dec 30 '13 at 18:24

2 Answers2

4

See using dispatchEvent to open new tab: {tested on chrome}

DEMO

$('a.site').each(function () {  
    var clk = document.createEvent("MouseEvents");
    clk.initMouseEvent("click", false, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
    this.dispatchEvent(clk);
});
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
-1

I wouldn't rely on using a triggered click to open a link, not all browsers will support it the same as if user clicks on it ( for obvious security reasons)

Would just loop through the elements and and grab the href, manipulate it the way you want, and pass result to window.open(url).

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • I know how to call window.open(), this does not solve what I was asking at all... you didn't even look at my JSFiddle and give me a code example; which if you would have, and tried it, it would yield the exact result of the problem I explained in this post. Not cool! – Taylor Evanson Dec 30 '13 at 21:04
  • I did look at fiddle...and code is ugly... and was trying to trigger `click` on links. Question is pretty long winded also...grow up. Sorry for trying to help! – charlietfl Dec 30 '13 at 21:08
  • You're right. I'm sorry... this issue has been so frustrating for me, and anyone i'd ask would say it can't be done. Thank you for trying to help, I appreciate it! – Taylor Evanson Dec 30 '13 at 21:37
  • window.open wouldn't work either so this solution doesn't help. There are good reasons to be frustrated because it really can't be done and that's because it's a bad practice that has been abused over the years, so any decent popup blocker will be triggered by any of these attempts (dispatchEvent also). Best case scenario you will be able to open only the first tab. – Mike Garcia Oct 16 '14 at 04:33