?
Solved

AUTOMATIC REFRESH ... but only once

Posted on 2003-03-21
15
Medium Priority
?
794 Views
Last Modified: 2007-12-19
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
Comment
Question by:Langdon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
15 Comments
 
LVL 5

Accepted Solution

by:
MMeijer earned 360 total points
ID: 8184891

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
 

Author Comment

by:Langdon
ID: 8185053
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
 

Author Comment

by:Langdon
ID: 8185056
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.

 
LVL 5

Expert Comment

by:MMeijer
ID: 8186072
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
 

Author Comment

by:Langdon
ID: 8187131
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
 
LVL 5

Expert Comment

by:MMeijer
ID: 8187698
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
 
LVL 6

Expert Comment

by:mattjp88
ID: 8187767
<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
 
LVL 6

Expert Comment

by:mattjp88
ID: 8187772
my script does not use cookies so the user will be reloaded even if they have their browser set to not accept cookies.
0
 
LVL 6

Expert Comment

by:mattjp88
ID: 8187824
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
 

Author Comment

by:Langdon
ID: 8188353
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
 

Author Comment

by:Langdon
ID: 8188393
will this work?


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

-L
0
 

Author Comment

by:Langdon
ID: 8188412
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
 
LVL 6

Expert Comment

by:mattjp88
ID: 8188667
what are you going to use this for?  there might be another way to accomplish it.

matt :-)
0
 

Author Comment

by:Langdon
ID: 8197315
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
 

Author Comment

by:Langdon
ID: 8197322
Tanks for the help
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question