Advanced Search

Page 1 of 3 123 LastLast
Results 1 to 10 of 30

Thread: Timed Redirect

  1. #1
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Timed Redirect

    I am trying to achieve that "timed redirect" you see on a lot of forums. They normally use it right after someones post. It says something like "Your browser should redirect you in 10 seconds,if it does not click here". Any help in figuring out how this is done would be appreciated.

  2. #2
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Don't know about others but you can achieve this with javascript pretty easily. Here's the code.

    Code:
    function redirect(){
    window.location.href = "http://somesite.somedomain/";
    }
    window.onload = setTimeout("redirect()", 10000);
    Redirects browser after 10 seconds.

    Hope that helps.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A meta refresh is more reliable.
    Code:
    <meta http-equiv="refresh" content="10;url=http://www.example.com/page2.html">
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh yes I forgot about that. Yes meta is more reliable than javascript because even if the user has javascript disabled meta can redirect you.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    However, not all browsers support it. Use both to maximise the chance that one will be supported.
    And that Javascript function would be better written as:
    Code:
    setTimeout(function(){window.location.replace("http://www.example.com/page2.html";}, 10000);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Thank you both, now about the meta refresh. Could that also be done with a header?

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Apparently so. No idea what the compatibility is though.
    Code:
    <?php $url = 'http://www.example.com/page2.html'; $timeout = 10; ?>
    
    <?php header('Refresh: ' . $timeout . ';url=' . $url); ?>
    <meta http-equiv="refresh" content="<?php echo($timeout) ?>;url=<?php echo($url); ?>">
    <script type="text/javascript">
      setTimeout(function(){window.location.replace("<?php echo($url); ?>";}, <?php echo($timeout * 1000); ?>);
    </script>
    One of those has got to do it.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by Twey
    One of those has got to do it.
    Probably not in IE, it will manage to find some way not to work...
    Anyway, I'm gonna do some tests with the header and find out.

  9. #9
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Ok, I did a little research and testing. Sending a Refresh header appears to work in the latest releases of IE,Firefox,and Opera. However, it is not (an official) part of HTTP and should not really be relied on. I am willing to risk just sending the header and providing a link backup. Hers's my test code if your interested.
    PHP Code:
    <?php
    header
    ('Refresh: 1;url=http://www.google.com');
    ?>
    Note: I also discovered that the counting begins once the content is fully loaded in the browser.

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh, aye. I forgot about the link backup.

    Well, so long as you've got that, I wouldn't worry about it too much.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •