New tab/window links in Safari with a redirect of the current page
<p>So I have this script here, it's main purpose is to open a specified link in a new tab/window and THEN redirect the current window (where the script has been fired, not the new tab). </p>

<p>It works as follow. You specify a link to listen on it for a click event, once it's clicked two instructions should fire up. New tab should be opened and the current window should be redirected.</p>

<p>The problem is that this script works in chrome, but in safari this script results just in a redirect to the specified url, the new tab/window does't not open up. </p>

<pre><code>function openTab(url) {
    // Create link in memory
    var a = window.document.createElement("a");
    a.target = '_blank';
    a.href = url;

    // Dispatch fake click
    var e = window.document.createEvent("MouseEvents");
    e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    a.dispatchEvent(e);
}

var aNode = document.getElementsByTagName('a'),
/* this is current link's url, just click copy link address you want to affect, and paste it here between ''*/
/* it supports multiple buttons with different links */
currentUrl = ['https://www.google.com/intl/en/ads/'],
/*this link loads in new tab */
newTabUrl = 'http://www.google.com/',
/* this link loads after the 'newTabUrl' is opened */
redirectUrl = 'http://www.youtube.com/';

        for (var t = 0; currentUrl["length"] &gt; t; t++) {
            for (i in aNode) {
                if (aNode[i].href &amp;&amp; aNode[i].href == currentUrl[t]) {
                    aNode[i].href = redirectUrl; // without this the page will be not      redirected in chrome (second instruction in the addEventListener, function below does not fire up somehow...)
                    aNode[i].addEventListener('click', function(){
                    openTab(newTabUrl);       
                    window.location = redirectUrl;
                    console.log('fired');  //debugging
                    });
                  console.log('affected!'); //debugging
                } 
              console.log(i); //debugging
            }
        }
</code></pre>
