Javascript ../path未重定向页面
我正在尝试将页面转到Javascript ../path未重定向页面,javascript,html,Javascript,Html,我正在尝试将页面转到search.html。根据页面的不同,search.html要么位于同一目录中,要么位于上面的目录中。我不明白为什么它没有重定向 以下是HTML代码: <form class="search" onsubmit="searchSite()"> <input id="search" title="Seach this site" type="text&qu
search.html
。根据页面的不同,search.html要么位于同一目录中,要么位于上面的目录中。我不明白为什么它没有重定向
以下是HTML代码:
<form class="search" onsubmit="searchSite()">
<input id="search" title="Seach this site" type="text" name="search"
placeholder="Search this site..">
</form>
搜索前,URL为:file:///C:Desktop/comp266/unit5/public_html/other/other.html
搜索后,URL为:file:///C:Desktop/comp266/unit5/public_html/other/other.html ?搜索=良好
但我希望URL是:
file:///C:Desktop/comp266/unit5/public_html/search.html
这是因为表单有其默认行为,即将站点重定向到表单的操作URL或查询URL,如您所见的查询参数。您可以通过在提交侦听器时返回false来防止此默认行为:
函数搜索站点(){
var search=document.getElementById(“search”).value.toLowerCase().split(“”);
搜索文章(搜索);
显示结果();
//加上这个
返回false;
}
如果在返回false之前可能出现错误,则更合适的方法是执行以下操作:
功能搜索站点(事件){
//加上这个
event.preventDefault();
var search=document.getElementById(“search”).value.toLowerCase().split(“”);
搜索文章(搜索);
显示结果();
//加上这个
返回false;
}
displayResults中的条件包含错误。它正在检查该位置是否包含人
,但其他
部分始终为真。您需要添加!==-1
。哦,非常感谢你指出这一点!!
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";
}