我已经继承了一个网站,在每个页面上调用一个JavaScript来预先配置每个外部链接并指向退出页面的链接。在exit.html中,同一脚本中的一个函数(confirmExit)提取原始的预期网址,并将其作为页面上的链接按ID (<p>Continue to:<a href="" id="exitLink"></a></p>)
如何切换到通用的自动重定向延迟
现在,用户不必点击exitLink,需要延迟自动重定向。就像“你现在将在10秒内被带到exitLink ...”
我见过setTimeout方法,<META HTTP-EQUIV="refresh" CONTENT="seconds;URL=the-other-url">
方法,甚至实现自动重定向的表单方法。问题是,那些似乎是用于硬编码,特定于页面的重定向。我一直无法弄清楚如何将这些适配到js或exit.html
页面以使它们工作。对不起,我的JavaScript学习曲线仍然很低,我似乎无法找到树木的森林!
任何解决方案将不胜感激! (除了PHP的 - 我不能使用)
这里的JavaScript:
window.onload = function() {
wrapExitLinks();
}
function wrapExitLinks() {
var whiteList = "^gov^mil^";
var exitURL = document.location.protocol + "//" + document.location.host + "/exit.html"; // Default exit is /exit.html from referring site
var currentBaseURL = document.location.protocol + "//" + document.location.hostname + document.location.pathname;
var links = document.getElementsByTagName("a");
var linkDest;
var linkTLD;
var govTLD;
/* Do not wrap links on intersitial exit page */
if (currentBaseURL != exitURL) {
for (var i in links) {
if (links[i].host) {
linkTLD = links[i].hostname.substr(links[i].hostname.lastIndexOf(".") + 1); // Extract top level domain from target link
linkDest = links[i].href;
if (whiteList.indexOf("^" + linkTLD + "^") == -1) {
linkDest = exitURL + "?url=" + encodeURIComponent(linkDest);
links[i].href = linkDest;
}
}
}
} else {
confirmExit();
}
}
function confirmExit() {
var queryString = decodeURIComponent(document.location.search.substr(1));
var linkDest = queryString.substr(queryString.indexOf("url=") + 4);
var exitLink = document.getElementById("exitLink");
/* Assume http:// if no protocol provided */
if (linkDest.indexOf("://") == -1) {
linkDest = "http://" + linkDest;
}
exitLink.href = linkDest;
exitLink.innerHTML = linkDest;
}
是的!这样可行。谢谢! – 2013-04-30 13:38:08