Redirect to a New Page using JavaScript

An easy way to redirect a user to a new Page using JavaScript is to use

window.location = "http://www.devcurry.com";

However the problem with this approach is that it adds an item to your browser history. So in browsers like IE, the user can access the history by hitting the back button, which can be confusing to users, as they will redirected back and forth.

image

If you want to avoid the back button, use ‘window.location.replace’ which loads the new page and replaces the current page in the user's History, with the new one, as shown below:

<head>
<
title>Redirect to a New Page</title>
<
script type="text/javascript">
window.location.replace("http://www.devcurry.com");
</script>
</
head>
<
body>
 

Now when you are redirected to the new page, there is no back button.

image

Note: If you are using ASP.NET, check my article Permanent Redirect in ASP.NET 4.0






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Alish | HoustonWebDesign said...

Hello mate,

It is not working in IE6 Browser. Also, Pop-up blocks this window.
How to solve this problem?
Thank you.
Regards,
Alish