这是你们几周前回答的先前问题的后续。 WordPress.org 应用程序中有我无法更改的 HTML。此 HTML 中的一个部分包含超链接到某个 URL 的文本。我希望 URL 重定向到我的引荐页面,以便在单击此链接时返回到我来自的任何页面。问题是某些东西阻止了将我的引荐来源网址字符串分配给 HREF。我在 Chrome 调试器中单步调试这个函数一百次,并尝试了各种方法(见下文)。无论如何,HREF 根本不会改变。知道为什么吗?这是代码,感谢您对此提供的任何帮助:
window.onload = function () {
document.querySelector(".button.wc-backward").onclick = function() {
var URLstring = document.referrer; // works fine
window.location.href = URLstring; // ref unchanged
setTimeout(function () {
window.location.href = URLstring; },100); // same result
return false;
}
}
最佳答案
你的jscode没有问题,我已经添加到这里了: Example on github
而且它正在工作。 (您将被重定向回 stackoverflow)。
<html>
<head>
</head>
<body>
<script>
window.onload = function () {
document.querySelector(".button.wc-backward").onclick = function() {
var URLstring = document.referrer; // works fine
window.location.href = URLstring; // ref unchanged
setTimeout(function () {
window.location.href = URLstring;
}, 100); // same result
return false;
}
}
</script>
<a href="www.google.com" class="button wc-backward">the link</a>
</html>
关于javascript - window.location.href 未分配/重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32622782/