【问题标题】:HTML meta redirect equivalent javascript code to the old urlHTML 元重定向等效 javascript 代码到旧 url
【发布时间】:2016-11-11 22:12:02
【问题描述】:

我把这个脚本放到index.html中

<script type="text/javascript">
    window.history.pushState('other.html', 'Other Page', 'other.html');
    window.history.pushState('index.html', 'Initial Page', 'index.html');

    window.addEventListener("popstate", function(e) {
        if (document.URL.indexOf("other.html") >= 0) {
            document.location.href = document.location;
        }
    });
</script>

在 other.html 文件中,我将此行放在 head 部分的元标记中。

http-equiv="refresh" content="0;URL='http://example.com/'

因此,当我第一次运行并单击浏览器后退按钮时,它将转到 other.html 并将我重定向到此链接 http://example.com/,当我再次单击浏览器后退按钮时,它会将我重定向到旧索引。 html

这工作正常。我需要在这里制作 HTML 元重定向的等效代码。我不想将此标记放入 other.html 中,并且我想创建现在执行 html 元标记的 javascript 代码。

这可能吗?任何帮助将不胜感激。

【问题讨论】:

  • 您已经知道如何使用location 进行重定向。你试过用那个吗?如果元标记方法有效,它有什么问题?

标签: javascript jquery html redirect


【解决方案1】:

other.html文件的head标签中添加如下代码:

<script type="text/javascript">
    window.location.href = "http://example.com";
</script>

相当于:

<meta http-equiv="refresh" content="0;URL='http://example.com/'" />

【讨论】:

    猜你喜欢
    • 2013-02-06
    • 2017-06-19
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多