Author -  Sai gowtham

How to redirect to another webpage in JavaScript

In this tutorial, 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 class="btn" >Click</button>

JavaScript


const btn = document.querySelector('.btn')


btn.addEventListener('click',()=>{
    window.location.href = "https://yournewurl.com"
})

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY