【问题标题】:Append Meta Data to URL将元数据附加到 URL
【发布时间】:2014-09-13 22:33:35
【问题描述】:

我有三个元变量:Concept、DocType 和 Path,我想将它们添加到我的 URL 中,以便在我的浏览器中找到类似于“http://www.mywebsite.com/page.html?concept=var1&doctype=var2&path=var3”的内容

现在我的页面上有以下代码,但我对 JS 很陌生,不确定这是否正确:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<meta name="concept" content="product"  />
<meta name="docType" content="template"  />
<meta name="Path" content="offer"  />

<script>
function refresh() {
  var concept = document.querySelector('meta[name="concept"]');
  var doctype = document.querySelector('meta[name="docType"]');
  var path = document.querySelector('meta[name="Path"]');

  this.document.location.href = "?concept=" + concept.content + "&doctype=" + doctype.content + "&path=" + Path.content;

}
</script>

</head>

<body>
Line of text
<script>
    if (location.search.indexOf("concept") !== -1) refresh();
</script>
</body>

</html>

这给了我以下错误,但我的网址没有更改:

回流:0.1ms 回流:0.11ms 回流:0.14ms 1406128139877 Services.HealthReport.HealthReporter WARN 未找到首选数据。

【问题讨论】:

    标签: javascript meta-tags


    【解决方案1】:

    使用querySelector 代替getElementByName,使用concept.content 代替concept.value。所以你的代码看起来像这样:

    <meta name="concept" content="product"  />
    <meta name="docType" content="template"  />
    <meta name="Path" content="offer"  />
    
    <script>
    function refresh() {
      var concept = document.querySelector('meta[name="concept"]');
      var doctype = document.querySelector('meta[name="docType"]');
      var path = document.querySelector('meta[name="Path"]');
    
      document.location.href = "?concept=" + concept.content + "&doctype=" + doctype.content + "&iapath=" + path.content;
    
    }
    </script>
    

    另外,iapath.content 是一个错误,请使用 path.content,这是你的变量名。

    另外,你必须在某处调用方法refresh;否则它不会运行:

    <body>
    Line of text
    <script>
        if (location.search.indexOf("concept") === -1) refresh();
    </script>
    </body>
    

    这段(静态)html 可以在我可以访问的所有浏览器(chrome、firefox、IE...)上的本地服务器上完美运行:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="concept" content="product"  />
        <meta name="docType" content="template"  />
        <meta name="Path" content="offer"  />
        <title>Test</title>
        <script>
        function refresh() {
            var concept = document.querySelector('meta[name="concept"]');
            var doctype = document.querySelector('meta[name="docType"]');
            var path = document.querySelector('meta[name="Path"]');
    
            document.location.href = "?concept=" + concept.content + "&doctype=" + doctype.content + "&iapath=" + path.content;
    
        }
        </script>
    </head>
    <body>
    Line of text
    <script>
        if (location.search.indexOf("concept") === -1) refresh();
    </script>
    </body>
    </html>
    

    【讨论】:

    • 谢谢 我尝试将我的代码更改为页面上的代码,但它似乎没有更改浏览器中的 url。
    • @mghurston 已更新。有没有错误? (按 Ctrl+shift+j)
    • 没有错误,这是我整页的代码...呃,不能在这个评论中放代码,所以基本上我只是在一行文本中添加了标准的 HTML、标题、正文标签。跨度>
    • @mghurston 好吧,你需要在某个地方打电话给refresh()
    • 不,仍然不起作用 - 更新了上面的代码 sn-p 以包含我在页面上的内容。
    猜你喜欢
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2018-08-27
    • 2015-04-22
    • 1970-01-01
    • 2019-08-05
    相关资源
    最近更新 更多