anmldr Posted February 2, 2010 Share Posted February 2, 2010 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 More sharing options...
justsomeguy Posted February 2, 2010 Share Posted February 2, 2010 All you can do is tell the browser not to cache the page. If you tell it not to cache it and it still uses a cached version for back and forward there's not really anything you can do about that. Link to comment Share on other sites More sharing options...
just2comment Posted February 2, 2010 Share Posted February 2, 2010 Maybe using XMLHttpRequest object? It's more complex than setting a browser, but adding something random as explained in http://www.w3schools.com/Ajax/ajax_example_suggest.asp would prevent caching at all (at least I've had specific problems on IE which were solved with this idea. Link to comment Share on other sites More sharing options...
anmldr Posted February 2, 2010 Author Share Posted February 2, 2010 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 Link to comment Share on other sites More sharing options...
Mencarta Posted February 2, 2010 Share Posted February 2, 2010 Maybe you can do: window.location.href = URL. Add that to body onload. Link to comment Share on other sites More sharing options...
jeffman Posted February 2, 2010 Share Posted February 2, 2010 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 More sharing options...
just2comment Posted February 2, 2010 Share Posted February 2, 2010 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 More sharing options...
anmldr Posted February 2, 2010 Author Share Posted February 2, 2010 <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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.