Jump to content

Force Reload On Each Visit


anmldr
 Share

Recommended Posts

How can I make sure that a page is reloaded or refreshed on each visit when the user taps the browser's back or forward button?I have tried these in the head--this doesn't work.<meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="Mon, 26 Jul 1997 05:00:00 GMT"/> <meta http-equiv="expires" content="0"><meta http-equiv="pragma" content="no-cache">I have not tried it in all browsers since I am really only interested in Safari and mobile Safari.Thanks,Linda

Link to comment
Share on other sites

Thanks for both of your answers. Perhaps I should explain why I want to do this and there may be another answer.When a page loads, I want to check a cookie and then change the class of a div depending on the value of the cookie. When someone clicks a link, everything works as expected. When they click the back button, the page is (I assume) the cached page and so the cookie is not checked in the onLoad event.Also, these pages will not be posted on the web on a server. Instead, they will be all in one folder on the user's computer.Thank you,Linda

Edited by anmldr
Link to comment
Share on other sites

The problem is that navigating to a page with backbutton does not trigger an onload event. If it did, no one would be asking about it.Navigating away from a page does trigger an unload event, however, and I still suggest exploring that as an alternative solution.

Link to comment
Share on other sites

According to http://www.hunlock.com/blogs/Mastering_The...With_Javascript, all you need is window.onbeforeunload = function () { // This fucntion does nothing. It won't spawn a confirmation dialog // But it will ensure that the page is not cached by the browser. }and it is true for IE and FF, but doesn't prevent caching in OP. Given the suggestion in post #6, I also added window.onunload = function () { // Needed in OP in order to avoid caching }which works ok for OP. I don't have Safari, so you'll have to see. Please report if you do. I've been playing around with this and I've used the two HTML docs I insert here just to have a "complete" set of files to play with. Warning: too many alerts...back.html contents:

<html>  <head>	<title>Example</title>	<script type="text/javascript">	  function someTitle()	  {		alert("Let's begin");		if (document.title != null)		{		  document.title = "First assigned title";		  alert("The title should be " + document.title);		}	  }	  window.onbeforeunload = function ()	  {		// This fucntion does nothing.  It won't spawn a confirmation dialog		// But it will ensure that the page is not cached by the browser.	  }	  window.onunload = function () 	  {		// Needed in OP in order to avoid caching	  }	</script>  </head>  <body onload=someTitle()>	<h1>Hi</h1>	<a href="forward.html">Next page</a>  </body></html>

forward.html contents (referred to in back.html "a" tag):

<html>  <head>	<title>Example</title>	<script type="text/javascript">	  function someTitle()	  {		alert("Let's begin");		if (document.title != null)		{		  document.title = "First assigned title";		  alert("The title should be " + document.title);		}	  }	</script>  </head>  <body onload=someTitle()>	<h1>Hi</h1>  </body></html>

I've been playing with browsers back and forward buttons just to verify that "back.html" is not cached while "forward.html" is.

Link to comment
Share on other sites

<script type="text/javascript"> window.onbeforeunload = function ()	  {		// This fucntion does nothing.  It won't spawn a confirmation dialog		// But it will ensure that the page is not cached by the browser.	  }	  window.onunload = function () 	  {		// Needed in OP in order to avoid caching. May also be needed in Safari.	  }

I made a note that this is probably also needed in Safari. My first test shows that it is working.Thanks,Linda
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...