您的位置:首页 > 其它

Reload the site when reached via browsers back button

2015-12-16 14:10 726 查看


Reload
the site when reached via browsers back button

up
vote3down
votefavorite
2

Problem: I have a site with dynamic content which needs to be reloaded every time the user sees it. This includes the use case when a user hits the back button on an another site and comes to the site needed to be reloaded. Most (all?) browsers don't refresh
the site after this event.

My solution (which isn't quite working):http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript
window.onbeforeunload = function () {
// This function does nothing.  It won't spawn a confirmation dialog
// But it will ensure that the page is not cached by the browser.
}


But it still doesn't refresh the page.

Any ideas what can affect/block the desired behavior? Respectively any other solution suggestions for this problem?

edit:

Set following:
Cache-Control   private, must-revalidate, max-age=0
Expires Sat, 26 Jul 1997 05:00:00 GMT
Pragma  no-cache


and:
<meta name="cache-control" content="no-cache" />
<meta name="expires" content="0" />
<meta name="pragma" content="no-cache" />


still no success.

javascript onbeforeunload
shareimprove
this question
edited Dec
5 '13 at 23:51




Benjamin
9,9771478152

asked Jan 28 '12 at 14:56





sorgenkind
41127

 
 
you shouldn't attempt to prevent the browser from caching the page. when i use the back button, i expect the page that
i was on to reappear as it was when i left it. but if that page has auto-updating elements then those should then continue to update when i return. – Dan
D. Jan
28 '12 at 15:14
add
a comment


8 Answers

activeoldestvotes

up vote15down
vote
You should use a hidden 
input
 as
a refresh indicator, with a value of "no":
<input type="hidden" id="refresh" value="no">


Now using jQuery, you can check its value:
$(document).ready(function(e) {
var $input = $('#refresh');

$input.val() == 'yes' ? location.reload(true) : $input.val('yes');
});


When you click on the back button, the values in hidden fields retain the same value as when you originally left the page.

So the first time you load the page, the input's value would be "no". When you return to the page, it'll be "yes" and your JavaScript code will trigger a refresh.

shareimprove
this answer
edited Oct
4 at 15:08





Joseph Silber
96.4k20163171

answered Oct 5 '13 at 9:12





Ali Alnoaimy
18819

 
1 
Worked for me. I had to use location.reload(true) to force a server reload otherwise it just reloaded from cache. – Kris Aug
7 '14 at 18:56
add
a comment
up vote2down
vote
this worked for me, in drupal 7 (php) whenever a user logs out, he can press the back button and able to visit the privileged pages. If a page is refreshed, then he is routed to a front page where he can't do anything.
<?php
//refresh the page after log-out and back button
if (!user_is_logged_in())
{

print '<input type="hidden" id="refreshed" value="no">
<script type="text/javascript">
onload=function(){

var e=document.getElementById("refreshed");
if(e.value=="no")e.value="yes";
else{e.value="no";location.reload();}
}
</script>';
}
?>


somebody posted here:
thanks i hope it help you too.

shareimprove
this answer
answered Feb 20 '13 at 6:09

community wiki

bherto39

 add
a comment
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: