2013-04-30 24 views
0

我已經繼承了一個網站,在每個頁面上調用一個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; 
} 

回答

0

您所需要的基本的腳本是:

setTimeout(function() { window.location = 'http://example.com'; }, 10000); 

這就是全部。在某處將其加入腳本。

+0

是的!這樣可行。謝謝! – 2013-04-30 13:38:08