SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Doing something like meta refresh in JavaScript

    I have a "splash" screen which displays a copyright and disclaimer. The first time someone accesses my site, that's the main part of the screen but when they click links to get to other content, I use a cookie to remember what link they clicked so when they return, they return to the same page. But I'd like the splash to show up every time and to be replaced after a brief delay by their last page. I have an onload handler in index.html which does:

    loadContent('contents', 'splash.shtml');
    var lastPage = xGetCookie('lastPage');
    if (lastPage) {
    loadContent('contents', lastPage);
    }

    but I don't get any delay there. In C, I'd do:

    if (lastPage) {
    sleep(10);
    loadContent('contents', lastPage);
    }

    In Tcl, I'd do:

    if {lastPage} {
    after 10000 loadContent 'contents' lastPage
    }

    which is nicer because it's asynchronous. How can I do this in JavaScript? Ideally, if the user clicked a link during the delay, I'd cancel the timer and not process their cookie but I can figure out how to cancel it later if I can get the delay to start with.

    I considered putting a <script> at the end of the splash that accesses the cookie and displays the last page but that doesn't give me any time delay control, either.

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    loadContent('contents', 'splash.shtml');
    var lastPage = xGetCookie('lastPage');
    if (lastPage) {
    // Wait 10,000 milliseconds = 10 secs
    setTimeout ( "loadContent('contents', "+ lastPage +")", 10000);
    }
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Close enough

    I had to do:

    setTimeout("loadContent('contents', \'"+lastPage+"\')", 3000);

    Or lastPage was treated as a variable name, not a value, and came up undefined.

    Thanks.

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But lastPage IS a variable.

    Glad it's working.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    But lastPage IS a variable.
    Yes, it is when I *build* the setTimeout argument but it's local and by not quoting it, it seemed that the timer handler or loadContent was seeing lastPage's value as a variable name. The failure occured when the timer fired.


Bookmarks

Posting Permissions

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