最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I switch between HTML pages in JavaScript - Stack Overflow

programmeradmin1浏览0评论

I've made a search bar in my web page and I'm trying to get the user input and pare 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>

I've made a search bar in my web page and I'm trying to get the user input and pare 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>
Share Improve this question edited Dec 6, 2020 at 21:08 Manu Sampablo asked Sep 12, 2020 at 18:06 Manu SampabloManu Sampablo 3505 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

First, you have to get the value from the input that you create and then you have to pare 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.

try with: 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/en-US/docs/Web/API/Window/open

发布评论

评论列表(0)

  1. 暂无评论