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
3 Answers
Reset to default 3First, 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