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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need Help with Javascript Refresher

    I need a Javascript refresh script that can refresh a page after a certain amount of milliseconds, but I want the script to stop refreshing when there is a certain tag or id on the page such as <div id="blue"> or something like that.

    How can I go about doing this?

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    I modified this: http://www.javascriptkit.com/script/...utofresh.shtml

    should do the trick

    Code:
    <script type="text/javascript">
        <!--
    
    /*
    Auto Refresh Page with Time script
    By JavaScript Kit (javascriptkit.com)
    Over 200+ free scripts here!
    */
    
        //enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
        var limit = "0:30"
    
        if (document.images) {
            var parselimit = limit.split(":")
            parselimit = parselimit[0] * 60 + parselimit[1] * 1
        }
    
        function beginrefresh() {
            if (!document.images) return
            if (!document.getElementById("blue") {
                if (parselimit == 1) window.location.reload()
                else {
                    parselimit -= 1
                    curmin = Math.floor(parselimit / 60)
                    cursec = parselimit % 60
                    if (curmin != 0) curtime = curmin + " minutes and " + cursec + " seconds left until page refresh!"
                    else curtime = cursec + " seconds left until page refresh!"
                    window.status = curtime
                    setTimeout("beginrefresh()", 1000)
                } else {
                    window.status = "Blue div found";
                }
            }
            }
    
            window.onload = beginrefresh
            //-->
    </script>
    Last edited by DanInMa; 01-02-2012 at 05:20 AM.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but I still can't seem to get it to work? That's the exact effect I'm looking for, but on a page like this, nothing happens in Firefox 9:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/loose.dtd">
    <html>
    <head>
    <script type="text/javascript">
        <!--
    
    /*
    Auto Refresh Page with Time script
    By JavaScript Kit (javascriptkit.com)
    Over 200+ free scripts here!
    */
    
        //enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
        var limit = "0:10"
    
        if (document.images) {
            var parselimit = limit.split(":")
            parselimit = parselimit[0] * 60 + parselimit[1] * 1
        }
    
        function beginrefresh() {
            if (!document.images) return
            if (!document.getElementById("blue")) {
                if (parselimit == 1) window.location.reload()
                else {
                    parselimit -= 1
                    curmin = Math.floor(parselimit / 60)
                    cursec = parselimit % 60
                    if (curmin != 0) curtime = curmin + " minutes and " + cursec + " seconds left until page refresh!"
                    else curtime = cursec + " seconds left until page refresh!"
                    window.status = curtime
                    setTimeout("beginrefresh()", 1000)
                } else {
                    window.status = "Blue div found";
                }
            }
            }
    
            window.onload = beginrefresh
            //-->
    </script>
    </head>
    <body>
    This page should refresh after 10 seconds unless the id blue is on the page. Since there is no id named blue on this page, it should be refreshing..?
    </body>
    Last edited by taylerzy; 01-02-2012 at 06:13 AM. Reason: minor word edit

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Why so complex? K.I.S.S.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function initRefresh( )
    {
        if ( document.getElementById("blue") == null )
        {
            setTimeout( window.location.reload, 5000 );
        }
    }
    window.onload = initRefresh;
    </script>
    </head>
    <body>
    .... may or may not have <div id="blue"> ...
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Why so complex? K.I.S.S.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function initRefresh( )
    {
        if ( document.getElementById("blue") == null )
        {
            setTimeout( window.location.reload, 5000 );
        }
    }
    window.onload = initRefresh;
    </script>
    </head>
    <body>
    .... may or may not have <div id="blue"> ...
    </body>
    </html>
    Wouldn't this script refresh the page once and then stop?


  •  

    Posting Permissions

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