...

View Full Version : How can I make my redirect faster?



thejackkelly
07-18-2010, 05:38 PM
Hello. When a user first visits /index.html, they select their city. On later visits, I redirect them to their city (ie. /vancouver.html). The script seems to run slowly, and loads the whole /index.html for a second before redirecting. How can I make it faster? I have tried placing my code 1) in the head just below the title, 2) in the head before anything else, 3) in <body onload="...">




function redirect() {

userCity = readCookie ("user_city");

if (userCity)
{
window.location.replace(userCity + ".html");
}
}


Could I use something like if (navigator.cookieEnabled) to run the redirect() function if cookie detected and somehow cancel loading the rest of index.html? (if no cookie, or not enabled, still load index). Or is their a more standard way?

I am a novice and really appreciate any help.

RandomUser531
07-18-2010, 05:52 PM
Where do you call the function? If you configure it as an inline function, it will run as soon as it's rendered:


(function() {

userCity = readCookie ("user_city");

if (userCity)
{
window.location.replace(userCity + ".html");
}
})();

thejackkelly
07-18-2010, 06:18 PM
Thanks (again!). I am struggling with the details of JS and your clear advice really helps with the issues. The inline function does the trick, and I will read up on it to make sure I know it for next time.

Best wishes.
Jack



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum