JavaScript offers several ways to display a different page in the current browser window.

Redirecting visitors with Javascript is straightforward. The simplest way is to use one of the methods given below.

location.href Method

Including the following script will immediately redirect visitors to the URL entered.

<script type="text/javascript">
     location.href='http://www.bloggingdeveloper.com';
</script>
 

location.replace Method

Including the following script will immediately redirect visitors to the URL entered.

<script type="text/javascript">
     location.replace('http://www.bloggingdeveloper.com/');
</script>

The difference between location.href and location.replace

The difference between location.href and location.replace is that the former creates a new history entry on the visitor's browser meaning that if they hit the back button, they can get in a 'redirection loop' which is usually undesirable and may have unwanted side effects.

Also consider checking the post that focuses on Javascript redirection with delay at JavaScript Url Redirect with Delay using setTimeout

Want automatic updates? Subscribe to our RSS feed or
Get Email Updates sent directly to your inbox!

Currently rated 4.2 by 10 people

  • Currently 4.2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Comments

November 19. 2007 20:11

Great info.  Thanks for sharing!

Rubber Stamps | Reply

October 22. 2009 23:38

Social comments and analytics for this post

This post was mentioned on Twitter by bloggingdev: location.href vs. location.replace - The Difference Between JavaScript Url Redirection Methods - http://su.pr/2t2aNK

uberVU - social comments | Reply

October 29. 2009 03:55

Thanks for sharing! Never knew the difference!

Jordan Andree | Reply

October 29. 2009 03:55

Thanks for sharing! Never knew the difference!

Jordan Andree | Reply

Add comment




(Will not be displayed!)








SponsoredTweets referral badge
Free CMS