我尝试进行一项练习,允许用户根据搜索字词重定向到特定的本地网页。出于某种原因,window.location.replace不会重定向。我尝试了一个window.location的变体,它在一个新标签中打开了登陆页面,该标签有效。任何帮助表示赞赏!
HTML
<form name="form1" onsubmit="myFunction()">
Street Address:
<input type="text" id="streetaddress" required="true"><br>
Township:
<input type="text" id="township" required="true">
<br>
<input type="submit" onclick="return myFunction()" name="Search" value="Search">
</form>
<p id="demo"></p>
JS
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "";
var nameValue1 = document.getElementById("streetaddress").value;
var nameValue2 = document.getElementById("township").value;
if (nameValue1.toUpperCase() === "1 MARJORAM DRIVE" && nameValue2.toUpperCase() === "LUMBERTON") {
window.location.replace("landingpage.html");
}
else {
document.getElementById("demo").innerHTML = "<font color='red'><p>0 results</p></font>";
return false;
}
}
</script>
更新
我修改了我的代码,实现了建议的解决方案,但仍然遇到了页面无法重定向的相同问题。我从原始代码改变的行是......
HTML
<form name="form1" onSubmit="return myFunction()">
<input type="submit" name="submit" class="submit" value="Search">
JS
if (nameValue1.toUpperCase() === "1 MARJORAM DRIVE" && nameValue2.toUpperCase() === "LUMBERTON") {
window.location.href = 'landingpage.html';
}
答案 0 :(得分:1)
只需更改window.location
变量
window.location = "http://www.newsite.com/path/to/page";
对于本地托管的页面,请使用以下命令:
window.location.href = "newlocation.html";