首页 > 解决方案 > JavaScript window.location.href 代码不起作用

问题描述

我知道这个问题以前被问过很多次,但没有一个修复对我有用。我做了一个window.location。href 之前的事情并且有效,但这个没有。我知道该功能会运行,因为我使用警报对其进行了测试。有人可以在这里看到任何问题吗?

<form>

    <input type="submit" name="agree" value="agree" onclick="fagree()">
    <input type="submit" name="disagree" value="Decline and go to google" onclick="fdisagree()">

</form>

<script type="text/javascript">
function fagree(){
window.location.assign("index.html")
localStorage.setItem("Terms", "true")
}
function fdisagree(){
window.location.href="https://www.google.com/"
return false;
}
</script>
</body>

标签: javascript

解决方案


如果不需要,请不要将其放入表单中(如果这是示例中的整个表单代码)。您在函数有机会触发之前提交它。

或者:

<form>

    <input type="button" name="agree" value="agree" onclick="fagree()">
    <input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">

</form>

<script type="text/javascript">
function fagree(){
window.location.assign("index.html")
localStorage.setItem("Terms", "true")
}
function fdisagree(){
window.location.href="https://www.google.com/"
return false;
}
</script>
</body>

推荐阅读