You need to be logged in to post in the forums. If you do not have an account, please sign up first.
force reload when pressing the back button
I am developing a web page that needs to reload from the sever any time a user presses the back button. I don't have a problem in IE, but Opera always wants to use the cached page, and not go back to the sever. I have added the headers in the head section:<meta http-equiv=expires content=-1>
<meta http-equiv=Cache-Control CONTENT=no-cache>
<meta http-equiv=Pragma CONTENT=no-cache>
But still, Opera wants to used the cached page. Any ideas?
Thanks!
Skip
21. March 2008, 23:41:05 (edited)
Today I finally solved this old, old issue and wanted to share my solution with the web-developer community! So here you go.
Put the following in your <head> tag:
<script>
function OperaReload() {
try {
var headElement = document.getElementsByTagName("head")[0];
if (headElement && headElement.innerHTML)
headElement.innerHTML += "<meta http-equiv=\"refresh\" content=\"1\">";
}
catch (e) {}
}
</script>
And in your body tag:
<body onUnload="OperaReload()">
Basically what I am doing is right before the page unloads, I am attaching a <META> tag that refreshes the page every second. Other browsers like Firefox, IE throw exception which I have enclosed in an empty try...catch statement.
HTH!
-rb79
Put the following in your <head> tag:
<script>
function OperaReload() {
try {
var headElement = document.getElementsByTagName("head")[0];
if (headElement && headElement.innerHTML)
headElement.innerHTML += "<meta http-equiv=\"refresh\" content=\"1\">";
}
catch (e) {}
}
</script>
And in your body tag:
<body onUnload="OperaReload()">
Basically what I am doing is right before the page unloads, I am attaching a <META> tag that refreshes the page every second. Other browsers like Firefox, IE throw exception which I have enclosed in an empty try...catch statement.
HTH!
-rb79
15. December 2008, 00:49:27 (edited)
I spent an entire Sunday struggling with this issue. I finally came to a simple solution that seems to be working for me. This solution is geared towards form submission, which is the issue I was trying to solve.
The solution I found was in using the window.name property. Initially, the first time a user enters the page, the window.name property is empty, so I do a check to verify that it's actually empty.
Next I do a little sniffing for Opera. If it's Opera:
1]Create event listener function (submit). I originally tried to run a function upon onunload but it didn't work for a form submission.
2]Created a function that will rename the window to "Opera" upon 'submit'.
3]Make the navigationMode 'compatible' (per Opera's instruction).
Here's how it works:
The user enters the page and the window.name property is empty. The user fills out the form and hits the submit button (or submit image). Upon selecting submit, the window.name is changed to 'Opera' and then the submission proceeds normally.
If the user hits the Back Button then the code at the beginning picks up that the window.name property is no longer empty. It then renames the window back to "" and sets a short timer to call the reload function. Seems like the timer did the trick for me.
The reload function fires and does just that, reloads the page. Unfortunately, all data in the form is reset as well but my first priority was actually having the page reload.
Hopefully this can help someone else before they go struggling down the same path as I did.
MJP
The solution I found was in using the window.name property. Initially, the first time a user enters the page, the window.name property is empty, so I do a check to verify that it's actually empty.
Next I do a little sniffing for Opera. If it's Opera:
1]Create event listener function (submit). I originally tried to run a function upon onunload but it didn't work for a form submission.
2]Created a function that will rename the window to "Opera" upon 'submit'.
3]Make the navigationMode 'compatible' (per Opera's instruction).
<script type="text/javascript">
function kbot_addListener(element, type, expression, bubbling){
bubbling = bubbling || false;
if(window.addEventListener) {
element.addEventListener(type, expression, bubbling);
return true;
} else if(window.attachEvent) {
element.attachEvent('on' + type, expression);
return true;
} else return false;
}
function kbot_reload(){
location.reload();
}
if(window.name!=''){
window.name='';
setTimeout("kbot_reload();",100);
}
// Opera browser detection
if(document.all && navigator.userAgent.indexOf("opera") == -1) {
function kbot_Opera() {window.name = 'Opera';}
history.navigationMode = 'compatible';
kbot_addListener(document.forms[0], 'submit', kbot_Opera);
}
</script>
Here's how it works:
The user enters the page and the window.name property is empty. The user fills out the form and hits the submit button (or submit image). Upon selecting submit, the window.name is changed to 'Opera' and then the submission proceeds normally.
If the user hits the Back Button then the code at the beginning picks up that the window.name property is no longer empty. It then renames the window back to "" and sets a short timer to call the reload function. Seems like the timer did the trick for me.
The reload function fires and does just that, reloads the page. Unfortunately, all data in the form is reset as well but my first priority was actually having the page reload.
Hopefully this can help someone else before they go struggling down the same path as I did.
MJP
21. July 2010, 23:09:58 (edited)
For what I need to achieve with the backbutton, I found I need a javascript reload() statement to make my webpage feature work in SeaMonkey and Firefox browsers. But in IE7 and Chrome, the reload() stops my webpage feature working. Then I found that in Opera, whether the reload() is there or not, my webpage feature just refuses to behave, and that the Opera user must follow the backbutton with a click on the browser's referesh button in order to see the feature.
Then I found that just sticking a codeless onUnload() event seem to make my webpage work ACROSS ALL BROWSERS. Thus I dont need the reload() nor mess around with browser speciifc codes!
I am not entirely clear though how this workaround seem to do thre trick so nicely so far.
And I spent the whole day needlessly writing a script to identify & report the browser and versions.
KSN
Then I found that just sticking a codeless onUnload() event seem to make my webpage work ACROSS ALL BROWSERS. Thus I dont need the reload() nor mess around with browser speciifc codes!
<head><script>
...
function unloadact() { }
</script></head>
<BODY onload="loadgo();" onUnload="unloadact();" >
I am not entirely clear though how this workaround seem to do thre trick so nicely so far.
And I spent the whole day needlessly writing a script to identify & report the browser and versions.
KSN
23. July 2010, 03:06:46 (edited)
spoke too soon. There can still be a backbutton problem if the advance to the next page is done automatically. But this problem only exist for Opera and Chrome, and if this is inconsistent with the solution which works for both Firefox and IE, then I guess it's not worth me investigating further & I can catch up with some sleep.