Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

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
0
Langdon
Asked:
Langdon
  • 8
  • 4
  • 3
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now