在URL中添加元数据

3
我有三个元变量: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>

这导致我在URL未更改的情况下出现以下错误:
回流:0.1毫秒 回流:0.11毫秒 回流:0.14毫秒 1406128139877 Services.HealthReport.HealthReporter WARN 找不到首选项数据。
1个回答

0

使用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
@mghurston 已更新。有任何错误吗?(按 Ctrl+shift+j) - soktinpk
没有错误,这是我的完整页面代码...唉,不能在这个评论中放置代码,所以基本上我只是添加了标准的HTML、Header、Body标签和一行文本。 - mghurston
不行,还是不起作用 - 已更新代码片段以包含我在页面上的内容。 - mghurston
谢谢,我一定做错了什么,URL 中没有显示额外的内容。我的文件名为 test.html,当我刷新页面时,它仍然只显示 test.html,而不是 test.html?concept=concept&doctype=doctype&path=path。 - mghurston
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接