How to open new tab in JavaScript without switching to the new tab?

前端 未结 8 1046
时光取名叫无心
时光取名叫无心 2020-11-29 04:11

How can I open a new tab using javascript without switching to the new tab?
For example, when a user clicks on a link a new tab is to be opened, but the user should stay

8条回答
  •  萌比男神i
    2020-11-29 04:37

    (function(a) {
        document.body.appendChild(a);
        a.setAttribute('href', location.href);
        a.dispatchEvent((function(e) {
            e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, 
                             true, false, false, false, 0, null);
            return e
        }(document.createEvent('MouseEvents'))))
    }(document.createElement('a')))
    

提交回复
热议问题