0

I have this code that does what I need. It opens the link in another tab but it moves to the newly opened tab leaving the current tab I'm in. I want it to keep my current tab active and newly opened tab just opened.

function loadpopunder() {
                var clicked = false;
                $('a').each(function() {
                    this.onclick = function() {
                        var a = document.createElement("a");
                        a.href = "http://sitetopopeninnewtab.com";
                        if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
                        {
                            a.target = "_blank";
                        }
                        if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
                        {
//                        var evt = new MouseEvent('click', {'view': window, 'bubbles': true, 'cancelable': true, });
                            var evt = document.createEvent("MouseEvents");
                        }
                        else {
//                        var evt = new MouseEvent('click', {'view': window, 'bubbles': true, 'cancelable': true, 'metaKey': true, 'ctrlKey': true});
                            var evt = document.createEvent("MouseEvents");
                        }
                        evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
                                true, false, false, false, 0, null);
                        if (clicked == false) {
                            a.dispatchEvent(evt);
                            clicked = true;
                        }
                    }
                });


if(window.location.href == 'http://mymainwebsite.com/'){
    loadpopunder();
}
MSaudi
  • 4,442
  • 2
  • 40
  • 65
  • This is controlled by the settings in your browser, which can not be manipulated using javascript. That means, opening a tab in background using javascript is (for almost all browsers) not possible. There is however a workaround by simulating a `ctrl+click`, which will only work on some browser versions, as described [here](http://stackoverflow.com/questions/10812628/open-a-new-tab-in-the-background) – ksbg Mar 19 '15 at 12:43
  • nothing is impossible. Maybe you have to open the tab in the background as in this question: http://stackoverflow.com/questions/10812628/open-a-new-tab-in-the-background so you wont loose the focus – Jordi Castilla Mar 19 '15 at 13:14

1 Answers1

0

This can't be done properly, because you depend on the browser settings. Even when you open a url in a new tab with target='_blank', if the browser is set to open new pages in a pop-up window, you wont get a new tab.

You should check some other SO answer about it here and here.

You can see on the comments that all browser have different behaviors. You can, however, manage new opened windows with javascript window object

Other thing that you can do, is check if the window is focused or not, by using the javascript visibility api.

Hope that helps.

Community
  • 1
  • 1
Yerko Palma
  • 12,041
  • 5
  • 33
  • 57