0

すべてのページで JavaScript を呼び出して、すべての外部リンクの前に終了ページへのリンクを追加するサイトを継承しました。exit.html では、同じスクリプト内の関数 (confirmExit) が元の意図した URL を抽出し、ID によってページ上のリンクとして提供されます。(<p>Continue to:<a href="" id="exitLink"></a></p>)

現在、ユーザーが exitLink をクリックする代わりに、遅延のある自動リダイレクトが必要です。「10 秒で exitLink に移動します…」のようなものです。

自動リダイレクトを実現するための setTimeout アプローチ、<META HTTP-EQUIV="refresh" CONTENT="seconds;URL=the-other-url">アプローチ、さらにはフォーム アプローチも見てきました。問題は、ハードコーディングされたページ固有のリダイレクトを意図しているように見えることです。exit.htmlこれらのいずれかをjsまたはページに適応させて機能させる方法を理解できませんでした。申し訳ありませんが、私はまだ 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;
}
4

1 に答える 1

1

必要な基本的なスクリプトは次のとおりです。

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

それで全部です。スクリプトのどこかに組み込んでください。

于 2013-04-30T13:00:20.023 に答える