javascript - New tab/window links in Safari with a redirect of the current page -
so have script here, it's main purpose open specified link in new tab/window , redirect current window (where script has been fired, not new tab).
it works follow. specify link listen on click event, once it's clicked 2 instructions should fire up. new tab should opened , current window should redirected.
the problem script works in chrome, in safari script results in redirect specified url, new tab/window does't not open up.
function opentab(url) { // create link in memory var = 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'), /* current link's url, click copy link address want affect, , paste here between ''*/ /* supports multiple buttons different links */ currenturl = ['https://www.google.com/intl/en/ads/'], /*this link loads in new tab */ newtaburl = 'http://www.google.com/', /* link loads after 'newtaburl' opened */ redirecturl = 'http://www.youtube.com/'; (var t = 0; currenturl["length"] > t; t++) { (i in anode) { if (anode[i].href && anode[i].href == currenturl[t]) { anode[i].href = redirecturl; // without page not redirected in chrome (second instruction in addeventlistener, function below not fire somehow...) anode[i].addeventlistener('click', function(){ opentab(newtaburl); window.location = redirecturl; console.log('fired'); //debugging }); console.log('affected!'); //debugging } console.log(i); //debugging } }
Comments
Post a Comment