How to Redirect to another page using JavaScript

← PrevNext →

There are many ways you can redirect a webpage to another in JavaScript. Some popular methods are, using window.location.href or simply window.location property to redirect to another page. In-addition, you can use window.open() method. Let's understand this with some working examples.

For example,

<script>
let redirectPage = () => {
    window.location = "https://www.encodedna.com/";
    
    // OR
    window.location.href = "https://www.encodedna.com/";
}
</script>
Try it

If you want, you can delay the redirect by few seconds. See this article.

Redirect Page but Open page in a New Tab

The window.location or window.location.href will open the redirected page on the current window. Your users will have to click the browser's back button to navigate back to the original page. So, here's an altenative. You can use window.open() method to open the redirected page in a new tab or window.

<script>
let redirectPage = () => {
    window.open("https://www.encodedna.com/", "_blank");
}
</script>
Try it

Note: You can learn more about window.open() method and its parameters here.

Redirect to Another Page using a Parameter

You can redirect to another page using a parameter in JavaScript. The URL of a webpage can have parameters. So, here’s how you can do this.

<script>
let redirectPage = () => {
    window.open("https://www.encodedna.com/?pg=2", "_blank");
}
</script>
Try it

Note: If you want, you can add multiple parameters to the redirect URL. For example,

https://www yourwebsite com/content/ocncontent?userid=43&contentid=9

Thanks for reading.

← PreviousNext →