Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How can I make my redirect faster?

    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="...">

    Code:
    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.

  • #2
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Where do you call the function? If you configure it as an inline function, it will run as soon as it's rendered:

    Code:
    (function() {
    
    userCity = readCookie ("user_city");
    
    if (userCity)
      {
     window.location.replace(userCity  + ".html");
      }
    })();

  • Users who have thanked RandomUser531 for this post:

    thejackkelly (07-18-2010)

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •