3

I'm trying to open new window in background. Here is my code:

function OpenInNewTab()  
{  
    var myChild= window.open('http://page.pl', '', 'width=,height=,resizable=no');  
    myChild.blur();
    window.focus();
} 

Usage:

<div id="player" style="width:450px;height:300px;">
    <img onclick="OpenInNewTab();" class="button" src="http://page.tv/images/foto.png" />
</div>

But it doesn't work. Where is a problem in this code?

Thanks.

Shaunak D
  • 20,588
  • 10
  • 46
  • 79
Newester
  • 1,457
  • 2
  • 14
  • 26

1 Answers1

-1

Copied from Amro: Open a new tab in the background?

UPDATE: By version 41 of Google Chrome, initMouseEvent seemed to have a changed behavior.

this can be done by simulating ctrl + click (or any other key/event combinations that open a background tab) on a dynamically generated a element with its href attribute set to the desired url

In action: fiddle

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

tested only on chrome

Community
  • 1
  • 1
Dustin Hoffner
  • 2,025
  • 1
  • 14
  • 15