所以我在这里有这个脚本,它的主要目的是在一个新的选项卡/窗口中打开一个指定的链接,然后重定向当前窗口(脚本被触发的地方,而不是新的选项卡)。
它的工作原理如下。您指定一个链接以在其上侦听单击事件,一旦单击它,就会启动两条指令。应打开新选项卡并重定向当前窗口。
问题是这个脚本在 chrome 中工作,但在 safari 中,这个脚本只会重定向到指定的 url,新的选项卡/窗口不会打开。
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"] > t; t++) {
for (i in aNode) {
if (aNode[i].href && 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
}
}