所以我在这里有这个脚本,它的主要目的是在新的选项卡/窗口中打开一个指定的链接,然后重定向当前窗口(脚本被触发的位置,而不是新的选项卡)。
它的工作原理如下。您指定了一个链接,用于监听点击事件,一旦点击它,就会触发两条指令。应打开新选项卡,并应重定向当前窗口。
问题是这个脚本在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
}
}