Avoid the Endless JavaScript Redirection Loop

Often, you'll find it useful to redirect a surfer to another page based on information collected by JavaScript, such as browser versions. However, you yourself have probably been caught in the annoying loop these redirections can often cause.

JavaScript redirects you to one page, and when you click the browser's back button to return to an earlier site, the intermediate JavaScript redirection forces you back to the same page. Typically, to create a JavaScript redirection, you use code similar to:

location.href = 'myRealPage.html'

This code loads myRealPage.html into the browser, but also keeps the original redirection page in the History list. Instead, try

location.replace('myRealPage.html')

Doing so replaces the redirection page in the browser's History list, saving site visitors some potential grief and minor cursing.

Source: Mike D. Jones
Viewed 4488 times