gpt4 book ai didi

javascript - 将元数据附加到 URL

转载 作者:行者123 更新时间:2023-12-04 20:06:29 25 4
gpt4 key购买 nike

我有三个元变量: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.1ms回流:0.11ms回流:0.14ms1406128139877 Services.HealthReport.HealthReporter WARN 未找到首选项数据。

最佳答案

使用 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>

关于javascript - 将元数据附加到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24898668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com