简体   繁体   中英

How do I switch between HTML pages in JavaScript

I've made a search bar in my web page and I'm trying to get the user input and compare it to a keyword. If it matches, show the user what he is searching for in another page.

The problem is that I don't know how to switch between pages of the HTML using JavaScript (being in page1.html and then take the user to page2.html if the keyword matches).

I've tried to implement href to a function and then running it when the search button is pressed but I haven't found a way to do it.

Here is the code:

<script>
   let search = getElementById("Search").value;
   if (search == "Keyword") {
      //a href="page2.html" - (this is was my main idea, but i haven't found a way to implement it correctly) 
   }
</script>

First, you have to get the value from the input that you create and then you have to compare between the value that you get it and the specific keyword that you want, and after that, you have to create a button to submit with it. Then you can try this line

window.location.href=“./Page2.htm”;

inside if condetion.

尝试使用: window.location.href=“./Page2.htm”;

You can use

window.open(stringURL);

to open the html in a new window.

To open a new tab:

window.open(stringURL, “_blank”);

For more info: https://developer.mozilla.org/en-US/docs/Web/API/Window/open

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