简体   繁体   中英

Why I am not going to that URL?

I'm making a search engine similar to Google and I've just started making it. But there's a problem! When I type "google translate", it does not go to https://translate.google.com when I've written a code in JavaScript for it and that is: 'window.location.href=' http://translate.google.com ';'.

Now see my code, this is my 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.");
  }
}

And this is my 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>

Please can anyone help me? I've also tried these:

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

But none of these helped me.

Try to use type="button" instead type="submit" . It will work because it won't submit the form, which is what submit does.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM