user3678479
user3678479

Reputation: 31

dispatchEvent not working in firefox

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

Upvotes: 3

Views: 4443

Answers (1)

user3270791
user3270791

Reputation: 201

Here is the working function (see):

function click(node){
  var evt=new MouseEvent('click',
     {'view':window,'bubbles':true,'cancelable':true});
  node.dispatchEvent(evt);
}   

Upvotes: 2

Related Questions