AUTOMATIC REFRESH ... but only once

Hi,

I am wondering if anyone has any insight on this... I've search all over the web but havent been able to find a truly sound answer...
I am looking for a script that will require no buttons or "click here"'s

I need to load the page directly from the server bypassing the users cache. Which will require a refresh (obviously)

Every solution I've tried have all had some problems... the solution needs to work on all browsers and not create a endless loop of refreshes. Preferrably with no <meta refreshes>

Anyknowhow?

-L
LangdonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MMeijerCommented:

cookie or querystring..

cookie:
-----------------------
function window.onload()
{
    if(document.cookie("refresh")=="yes")
    {
        document.cookie("refresh")="no";
        location.href=location.href;
    }
}
-----------------------

querystring:
-----------------------
function window.onload()
{
    if(location.href.indexOf('refresh=yes')!=-1)
    {
        location.href=location.href.replace('refresh=yes','refresh=no');
    }
}
-----------------------
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LangdonAuthor Commented:
Sorry, I appreciate your answers but am unclear on what to do with them

JS is not my forte... Could you explain a little bit more...

-L
0
LangdonAuthor Commented:
Sorry, I appreciate your answers but am unclear on what to do with them

JS is not my forte... Could you explain a little bit more...

-L
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

MMeijerCommented:
The function window.onload() will execute when the page is loaded.
cookie:
-----------------------
//set a cookie if the page is loaded for the first time
//document.cookie("refresh")="yes"
// if the cookie is "yes" it will become "no"
//and the procedure will not set the current
//location.href to location.href.
function window.onload()
{
   if(document.cookie("refresh")!="yes")
   {
       document.cookie("refresh")="no";
       location.href=location.href;
   }
}
-----------------------

querystring:
-----------------------
//check if there is a value, "refresh=yes"
//in the current location.href
//If so then follow the procedure.
//This will set the location.href to location.href
//replacing the value "refresh=yes" to "refresh=no"
function window.onload()
{
   if(location.href.indexOf('refresh=yes')!=-1)
   {
       location.href=location.href.replace('refresh=yes','refresh=no');
   }
}
-----------------------
0
LangdonAuthor Commented:
From what I understand this function will execute all by itself when the page loads?

If this is correct passing refresh as 'yes' will only perform a soft reload of the page and will grab the info from the users cache. Ideally I would like to use

 window.location.reload(true)

but I am having trouble implementing this to work only one time... as it stands the window.location.reload(true) will loop endlessley.

But thanks for the clairification

-L
0
MMeijerCommented:
Hi,

The querystring function doesn't work with the "location.reload" method. So try the cookies.

I forgot the following function, in the previous example..
//extend document object with cookies method.
function document.cookies(sKey)
{
     var strRet='';
     if(document.cookie.indexOf(sKey)!=-1)
     {
          strRet = document.cookie.substr(document.cookie.indexOf(sKey)+sKey.length+1);
          strRet = strRet.substring(0,strRet.indexOf(';'));
     }
     return strRet;
}


function window.onload()
{
     if(document.cookies('refresh')!='no')
     {
          document.cookie = 'refresh=no';
          location.reload(true)
     }
}
0
mattjp88Commented:
<head>
<script>
function checkReload() {
var url=document.location.href;
var url1=url.split('?');
if (url1[1]!="reload") {document.location.href=url+"?reload"}
if (url1[1]=="reload") {document.write('reloaded')}
}
</script>
</head>
<body onload=checkReload()>
</body>

matt :-)
0
mattjp88Commented:
my script does not use cookies so the user will be reloaded even if they have their browser set to not accept cookies.
0
mattjp88Commented:
you can actually get rid of:

if (url1[1]=="reload") {document.write('reloaded')}

it was just for testing the script.  i forgot to take it out.
0
LangdonAuthor Commented:
The points are so close to being your MMeijer... but just one more thing...

function document.cookies(sKey)
{
     var strRet='';
     if(document.cookie.indexOf(sKey)!=-1)
     {
          strRet = document.cookie.substr(document.cookie.indexOf(sKey)+sKey.length+1);
          strRet = strRet.substring(0,strRet.indexOf(';'));

************** I NEED TO DESTOY THE COOKIE HERE ****************    

     }
     return strRet;
}

This way the reload will execute every time the user returns to the page, but will not execute a loop... this is what I am looking for... I have no experience with JS so I am unsure how to destory a cookie....

Thanks for all your help


mattjp88... your fuction (although good) will not produce the results I need... it will only perform a soft reload grabbing the info from the users cache not the server, thanks though


0
LangdonAuthor Commented:
will this work?


document.cookie = "firstname=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";  

-L
0
LangdonAuthor Commented:
Disregard that comment above.... I wasn't really thinking destroying the cookie in that part of the function doesnt make much sense... does it

(Edited by Computer101)... I am never going to get this working...
0
mattjp88Commented:
what are you going to use this for?  there might be another way to accomplish it.

matt :-)
0
LangdonAuthor Commented:
I figured it out... I wasn't able to deal with the cookies in JS so I came up with a PHP solution:

<?
     session_start();
     if(session_is_registered("reload"))
          {
               session_unregister("reload");
          }
     else
          {    
               session_register("reload");
               echo "<script language = 'javascript'>window.location.reload(true)</script>";
          }
               
?>

works like a charm... and it only took me 3 days to figure out... :( the simplicity of it all is so..... well simple...

I guess i get the points...

-L
0
LangdonAuthor Commented:
Tanks for the help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.