为什么我不去那个网址?

时间:2015-09-03 15:50:02

标签: javascript html url web

我正在制作类似于谷歌的搜索引擎,我刚开始制作它。 但是有一个问题!当我输入“谷歌翻译”时,当我用JavaScript编写代码时,它不会转到https://translate.google.com,而是:'window.location.href ='http://translate.google.com';'。

现在看我的代码,这是我的search.js:

function search_Results (form) {

  var search_term = form.search_box.value; //This is the search term

  //About out company
  if(search_term == "About" ||
     search_term == "about")
     {
         alert("About");
     }

  //Google translate
  else if(search_term == "Google translate" ||
          search_term == "google translate"// ||
          //search_term == "Google Translate" ||
         // search_term == "google Translate"
          )
          {
              location.href='http://www.example.com';
          }
  else
  {
      alert("Extremely sorry! Your search term: '"+search_term+"' does not matches any search documents.");
  }
}

这是我的searchengine.html:

<!DOCTYPE html>
<html>
<head>
 <!----The search script---->
 <script src="Search/search.js"></script>
 <!----The style scripts---->
 <link rel="stylesheet" type="text/css" href="GUI/button.css">
 <link rel="stylesheet" type="text/css" href="GUI/heading.css">
 <link rel="stylesheet" type="text/css" href="GUI/txtinput.css">
</head>
<body>
 <center>
 <!----Heading---->
 <h1 class="heading">Search Engine</h1>
 <!----End that---->
 <!----Search options---->
 <a href="searchImages.html"> 
  <button class="btn">Images</button>
 </a>
 <a href="searchVideos.html">
  <button class="btn">Videos</button>
 </a>
 <!----End of the search options---->
 <!----The main search form---->
 <form name="wow" method="">
  <br><br><br>
  <input type="text" name="search_box" value="Enter Your Search Term" class="css-input">
  <br><br>
  <input type="submit" value="Search" onClick="search_Results(this.form)" class="btn">
 </form>
 <div id="div1">
 </div>
 <!----End of the search form---->
 </center>
</body>
</html>

请有人帮帮我吗?我也试过这些:

window.location.href="...";
window.location="...";
location.href="...";
location="...";

但这些都没有帮助我。

1 个答案:

答案 0 :(得分:4)

尝试使用type="button"代替type="submit"。它会起作用,因为它不会提交表单,这就是提交的内容。

<input type="button" value="Search" onClick="search_Results(this.form)" class="btn">