T
T
Tatarin2015-02-26 21:29:06
JavaScript
Tatarin, 2015-02-26 21:29:06

How to open a tab in chrome without losing focus on the current tab?

broken code example:

window.open("ya.ru", "_blank").blur();
window.focus();

googled, googled, didn't find a working solution

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2015-02-27
@tatarin

But it works for chrome like a current.

function openNewBackgroundTab(){
    var a = document.createElement("a");
    a.href = window.location.pathname;
    var evt = document.createEvent("MouseEvents");
    //the tenth parameter of initMouseEvent sets ctrl key
    evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
                                true, false, false, false, 0, null);
    a.dispatchEvent(evt);
}

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if(!is_chrome)
 {
  var url = window.location.pathname;
  var win = window.open(url, '_blank');
 } else {
  openNewBackgroundTab();
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question