Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ../path未重定向页面_Javascript_Html - Fatal编程技术网

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";
}