How to Redirect to another page using JavaScript

← PrevNext →

In JavaScript, you can use window.location.href or simply window.location property to redirect to another page. This is a quick JavaScript tip for beginners.

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 →


Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.
Delivered by FeedBurner