mariobros mariobros - 2 months ago 33
Javascript Question

Open new window tab without focus on new tab

There's a way to open new tab (using jQuery or Javascript) and remain on main page?

i've tried this code but focus is on opened tab:

function openNewBackgroundTab(){
var a = document.createElement("a");
a.href = "http://www.google.com/";
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);
}

Answer

What you seek is called a "pop-under" window

  • Open a new window using window.open()
  • Lose focus of the new window by using window.blur()
  • Then return focus to your existing window using window.focus()

However, it's not a guarantee as user browser settings may override this behavior, especially pop-up blockers.