[英]../path is not redirecting page
I'm trying to the page to search.html
.我正在尝试将页面转到search.html
。 Depending on what page it is, search.html is either located in the same directory or a directory above.根据它是什么页面,search.html 要么位于同一目录中,要么位于上面的目录中。 I can't figure out why it's not redirecting.我不明白为什么它没有重定向。
Here is HTML code:这是 HTML 代码:
<form class="search" onsubmit="searchSite()">
<input id="search" title="Seach this site" type="text" name="search"
placeholder="Search this site..">
</form>
Here is JS code:这是JS代码:
function searchSite() {
var search = document.getElementById("search").value.toLowerCase().split(" ");
searchArticles(search);
displayResults();
}
function displayResults() {
if (window.location.href.indexOf("humans") !== -1
|| window.location.href.indexOf("other")){
window.location.href = "../search.html";
console.log(window.location.href);}
else
window.location.href = "search.html";
}
Before searching the URL is: file:///C:Desktop/comp266/unit5/public_html/other/other.html
搜索之前的网址是: file:///C:Desktop/comp266/unit5/public_html/other/other.html
After searching the URL is: file:///C:Desktop/comp266/unit5/public_html/other/other.html ?search=good
搜索后网址为: file:///C:Desktop/comp266/unit5/public_html/other/other.html ?search=good
But I want the URL to be: file:///C:Desktop/comp266/unit5/public_html/search.html
但我希望 URL 为: file:///C:Desktop/comp266/unit5/public_html/search.html
This is because form has its default behavior that redirect site to form's action URL or query URL with query params as you see.这是因为表单有其默认行为,如您所见,将站点重定向到表单的操作 URL 或带有查询参数的查询 URL。
You can prevent this default behavior by return false on submit listener:您可以通过在提交侦听器上返回 false 来防止此默认行为:
<form class="search" onsubmit="return searchSite()"> <!-- ADD THIS -->
<input id="search" title="Seach this site" type="text" name="search"
placeholder="Search this site..">
</form>
function searchSite() {
var search = document.getElementById("search").value.toLowerCase().split(" ");
searchArticles(search);
displayResults();
// ADD THIS
return false;
}
A more proper way is do preventDefault
in case there may be bug occurs before return false:更正确的方法是做preventDefault
以防在 return false 之前可能发生错误:
<form class="search" onsubmit="return searchSite(event)"> <!-- ADD THIS -->
<input id="search" title="Seach this site" type="text" name="search"
placeholder="Search this site..">
</form>
function searchSite(event) {
// ADD THIS
event.preventDefault();
var search = document.getElementById("search").value.toLowerCase().split(" ");
searchArticles(search);
displayResults();
// ADD THIS
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.