#1
  1. No Profile Picture
    Designer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    258
    Rep Power
    12

    Question Refresh with META tag ONCE only?


    I understand that this is the tag for refreshing a web page:

    <meta http-equiv=REFRESH content=2>

    However, I would like to only refresh each page once for my company intranet site. Would like to refresh the page 2 seconds after it loads and then that is it.

    Does anyone have a simple code for this? Preferrably HTML.

    Thank in advance,

    Steady
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Location
    Italy, EU
    Posts
    517
    Rep Power
    12
    Without server-side scripts I can figure only javascript.
    Look for a cookie:
    if exists don't write the refresh code;
    if it doesn't exist set the cookie and write the refresh code.

    Hope it helps!
    Cheers, Dave
  4. #3
  5. No Profile Picture
    Designer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    258
    Rep Power
    12
    This may be a bit too advanced for me, how involved is setting a cookie?
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Location
    Italy, EU
    Posts
    517
    Rep Power
    12
    You can try to use the following code putting it in the HEAD of your HTML file... I've tested it and seems to work:
    as it is set, it will refresh the page in 2 secs if the cookie isn't set and set the cookie, if the cookie is set it won't do anything. I haven't set the expire date (so the cookie is deleted when all the browser's windows are closed)...
    Code:
    <SCRIPT LANGUAGE="Javascript">
    <!-- Hide from older browsers
    function setCookie(name, value, expire) {
    	strCookie = name + "=" + escape(value);
    	if (expire) strCookie += ";
    	expires=" + expire.toGMTString();
    	document.cookie = strCookie;
    }
    
    function getCookie(Name) {
    var search = Name + "="
    if (document.cookie.length > 0) {
    	// if there are any cookies
    	offset = document.cookie.indexOf(search)
    	if (offset != -1) {
    		// if cookie exists
    		offset += search.length
    		// set index of beginning of value
    		end = document.cookie.indexOf(";", offset)
    		// set index of end of cookie value
    		if (end == -1) end = document.cookie.length;
                    return unescape(document.cookie.substring(offset, end))
                }
            }
        }
    
    var temp = getCookie('refreshed');
    if(temp!=1){
    	setCookie('refreshed', 1);
    	document.write("<meta http-equiv=REFRESH content=2>");
    }
    // End hiding -->
    </script>
    I hope this helps you!!
    Cheers, Dave
  8. #5
  9. No Profile Picture
    Designer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    258
    Rep Power
    12
    Thanks Dave, where do I set the expiration date if at all?

    Daniel
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Location
    Italy, EU
    Posts
    517
    Rep Power
    12
    To set the expiration date you've to add/change the following lines:
    Code:
    var exp = new Date ();
    exp.setTime(exp.getTime() + (4 * 3600 * 1000));
    if(temp!=1){
    	setCookie('refresh', 1, exp);
    The time must be expressed in milliseconds, so, in the example, the cookie will expire in 4 hours...
    Cheers, Dave
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    1
    Rep Power
    0

    tried it but got an error


    I tried the above cookies script.... but I got a javascript error message that line 9 (character 29) contained an error.....

    How can I solve this so I can make this script work?

    Greetings,
    Manuela
  14. #8
  15. Cunning Linguist
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    I used to live at home, now I stay at the house
    Posts
    10,180
    Rep Power
    2039
    Alternatively, instead of having to refresh the page, have you tried this...
    Code:
    META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    It'll mean that the page can't be stored in cache, and should therefore load fresh everytime you view it, and not need the refresh...

IMN logo majestic logo threadwatch logo seochat tools logo