How to redirect to another webpage in JavaScript
In this post we will give you information about How to redirect to another webpage in JavaScript. Hear we will give you detail about How to redirect to another webpage in JavaScriptAnd how to use it also give you demo for it if it is necessary.
we are going to learn about how to redirect a user from one page to another page in JavaScript.
Consider we have contact form whenever a user submits a form we need to redirect the user from/contact-page to /home-page or some other page. In such cases, we can use the href property or replace() method in JavaScript.
Using href
window.location.href = "https://google.com"
Just open your console and run the above code, you will be redirected from the current page to google.com.
Using replace() method
window.location.replace("https://google.com")
The difference between replace() and href is, the replace() method doesn’t save the current page in the session history, so that, we can’t use the browser back button to navigate back to the previous page.
Redirecting from one page to another page by using a button click
Example:
Html
<button >Click</button>
JavaScript
const btn = document.querySelector('.btn')btn.addEventListener('click',()=>{ window.location.href = "https://yournewurl.com"})
Hope this code and post will helped you for implement How to redirect to another webpage in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs