如何在博客中为所有外部链接创建重定向页面?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (52)

我想在博客中创建一个页面,该页面将在5-10秒后重定向。就像php一样,我们可以通过该页面重定向任何外部URL(下载链接)。

<h3>Page Will Redirect After <span id="countdown">10</span> seconds</h3>

<!-- JavaScript part -->
<script type="text/javascript">
    
    // Total seconds to wait
    var seconds = 20;
    
    function countdown() {
        seconds = seconds - 1;
        if (seconds < 0) {
            // Chnage your redirection link here
            window.location = "https://www.google.com";
        } else {
            // Update remaining seconds
            document.getElementById("countdown").innerHTML = seconds;
            // Count down using javascript
            window.setTimeout("countdown()", 1000);
        }
    }
    
    // Run countdown function
    countdown();
    
</script>

With this code i made a page (example: abc.blogspot.com/p/go.html) and i add link in post "<a href="http://abc.blogspot.com/p/go.html?redirect=www.facebook.com">LINK</a>"

but it's not redirect to facebook because in code mention : "window.location = "https://www.google.com";"

如何在博客中制作带有计时器的重定向页面,并使用html代码重新发布帖子中的所有外部链接?

提问于
用户回答回答于

整页网址的下载链接显示如下:

<a href="http://abc.blogspot.com/p/go.html?redirect=http://www.mediafire.com/file/code.rar">LINK</a>
用户回答回答于

你需要从网址获取“查询参数”并将Google链接更改为该链接。

var seconds = 20;
var urlParams = new URLSearchParams(window.location.search);
var redirectLink = urlParams.get('redirect');
// CAUTION: assumes the redirectLink passed in URL is a valid URL.
// that is, it starts with a protocol - http://,  https://

function countdown() {
    seconds = seconds - 1;
    if (seconds < 0) {
        // Chnage your redirection link here
        window.location = redirectLink;
    } else {
        // Update remaining seconds
        document.getElementById("countdown").innerHTML = seconds;
        // Count down using javascript
        window.setTimeout("countdown()", 1000);
    }
}

// Run countdown function
countdown();

<h3>Page Will Redirect After <span id="countdown">10</span> seconds</h3>

扫码关注云+社区

领取腾讯云代金券