【问题标题】:How to do a http-equiv redirect that preserves query_string and fragment_id如何进行保留 query_string 和 fragment_id 的 http-equiv 重定向
【发布时间】:2012-03-07 06:32:10
【问题描述】:

在 Javascript 中,我可以像这样重定向和保留查询字符串和片段 ID:

window.location = "NEW_LOCATION" + window.location.search + window.location.hash;

对于没有 Javascript 的网站,您可以使用 http-equiv 元标头。但这会删除查询字符串和片段 ID:

<head>
    <meta http-equiv="Refresh" content="300; url=NEW_LOCATION" />
</head>

有没有一种方法可以使用 http-equiv="refresh" 来保留查询字符串和片段 ID?

【问题讨论】:

  • 将查询字符串和片段追加到 NEW_LOCATION? NEW_LOCATION#fragment?querystring
  • @Frederik,我不知道它们是什么。用户可以设置它们,我希望它们可以带到新位置。

标签: javascript html redirect


【解决方案1】:

您可以使用 JavaScript 使用查询字符串和哈希来更新元标记。

更新 对于 IE8+,更好的方法是使用 noscript 标记和 JavaScript 驱动的重定向。在 html 元素上添加重定向作为 data-destination 属性,以便脚本可以轻松抓取它。

<!DOCTYPE html>
<html data-destination="http://stackoverflow.com">
  <head>
    <noscript><meta id="redirect" http-equiv="refresh" content="0; url=http://stackoverflow.com"></noscript>
  </head>

  <body>
    This page has moved. Redirecting...
    
  <!-- Redirect in JavaScript with meta refresh fallback above in noscript -->
  <script>
  var destination = document.documentElement.getAttribute('data-destination');
  window.location.href = destination + (window.location.search || '') + (window.location.hash || '');
  </script>
  </body>
</html>

【讨论】:

    【解决方案2】:

    如果没有将正确的 url 放入 HTML 标记(或直接发送 Refresh 标头)的服务器端脚本语言,则不然。

    【讨论】:

    • 这也是我的信念。你有什么明确的可以引用的吗?
    【解决方案3】:

    我正在使用以下脚本(在表单之后,但在正文结束之前)。所需的 URL(“hidAutoURL”)首先存储在隐藏变量中(来自服务器端)。

    document.write 用于更新meta

    <BODY>
    
        <FORM>
        </FORM>
    
        <script>
            function getAutoURL()
            {
                var url = document.getElementById('hidAutoURL').value;
                return url;
            }
    
            //Update META for auto-refresh
            var configuredTime = document.getElementById('hidRefrTime').value;
            var content = configuredTime + ';url=' + getAutoURL('url');
            document.write('<meta http-equiv="refresh" content="'+content + '"/>');
    
        </script>
    
    </BODY>
    

    参考文献

    1. What is the correct way to write HTML using Javascript?
    2. Changing the content of meta refresh does not change refreshing time
    3. Using document.write

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 2011-03-24
      • 2021-01-08
      相关资源
      最近更新 更多