3

I was trying to open a new tab inside my JavaScript. It works as expected in Chrome, but when it comes to Firefox it does nothing.I know window.open() can be used to open a tab, but my intention is to keep the focus on the current page. I have spent my entire day searching for a fix for this, but all I could see is people claiming it is a bug in Firefox. I wanted to know if there is any work around for this. Here is the piece of code I am using.

var a = document.createElement("a");
a.href = popup_url;//this comes from the function's argument
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
         true, false, false, false, 0, null);
a.dispatchEvent(evt);
user3678479
  • 31
  • 1
  • 3
  • possible duplicate of http://stackoverflow.com/questions/809057/how-do-i-programmatically-click-on-an-element-in-firefox – Noino Jul 02 '14 at 13:05
  • Yes. I have seen that post and 4 more, but none of those solutions worked for me.So I am posting a new question here. – user3678479 Jul 03 '14 at 05:05
  • Well clearly there is no easy fix. Firefox just simply does not allow this kind of behavior. Even trying to ['pop-under'](http://stackoverflow.com/questions/2181464/i-need-to-to-open-a-new-window-in-the-background-with-javascript-and-make-sure) doesn't work in my firefox – Noino Jul 03 '14 at 08:39
  • Ok, Thank you for your suggestions, but I really want to fix this issue somehow, it is quite important. – user3678479 Jul 03 '14 at 09:22

1 Answers1

2

Here is the working function (see):

function click(node){
  var evt=new MouseEvent('click',
     {'view':window,'bubbles':true,'cancelable':true});
  node.dispatchEvent(evt);
}   
Community
  • 1
  • 1
user3270791
  • 191
  • 1
  • 4