[英]How to refresh page on back button click?
我使用.htaccess將我的所有流量路由到單個index.php文件。 下面的代碼用於指示流量,但由於某種原因,它不能與后退按鈕一起使用。 我不知道如何讓后退按鈕工作。 我已經嘗試了各種各樣的東西,谷歌搜索了一點,沒有一個有效,所以我希望有人在這里可以提供幫助!
<?php
if(isset($_GET['parameters'])) {
if($_GET['parameters'] == "repair")
include 'repair.html';
else if($_GET['parameters'] == "training")
include 'training.html';
else if($_GET['parameters'] == "products")
include 'products.html';
else if($_GET['parameters'] == "about")
include 'about.html';
else if($_GET['parameters'] == "employees")
include 'employees.html';
else if($_GET['parameters'] == "training")
include 'training.html';
else if($_GET['parameters'] == "newaccount")
include 'newaccount.html';
else if($_GET['parameters'] == "contact")
include 'contact.html';
else if($_GET['parameters'] == "recommended")
include 'recommended.html';
else if($_GET['parameters'] == "careers")
include 'careers.html';
else
include 'home.html';
}
else
include 'home.html';
?>
到目前為止,我已嘗試過但未能使用: window.onbeforeunload
body onunload=""
必須有一種方法來onunload=location.reload()
或其他東西! 不知何故必須知道語法!!
試試這個...未經測試。 我希望它對你有用。
制作一個新的php文件。 您可以使用后退和前進按鈕,頁面上的數字/時間戳始終更新。
<?php
header("Cache-Control: no-store, must-revalidate, max-age=0");
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo time();
?>
<a href="http://google.com">aaaaaaaaaaaaa</a>
要么
找到另一個解決方
當用戶點擊后退按鈕時,應該觸發onload事件。 不是通過JavaScript創建的元素將保留其值。 我建議在INPUT TYPE =“hidden”設置中保留動態創建元素中使用的數據的備份,以顯示:none然后onload使用輸入值將動態元素重建為它們的方式。
<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();}
}
最新的解決方案是使用PerformanceNavigation接口 :
if(!!window.performance && window.performance.navigation.type === 2)
{
console.log('Reloading');
window.location.reload();
}
值2表示“通過導航到歷史記錄訪問頁面”。
在此處查看瀏覽器支持: http : //caniuse.com/#search=Navigation%20Timing%20API
隱藏的輸入解決方案在Safari中不適用於我。 下面的解決方案有效,來自這里 。
window.onpageshow = function(event) {
if (event.persisted) {
window.location.reload()
}
};
我找到了兩種方法來處理這個問題。 選擇最適合您的情況。 在Firefox 53和Safari 10.1上測試的解決方案
1.檢測用戶是否正在使用后退/前進按鈕,然后重新加載整頁
if (!!window.performance && window.performance.navigation.type === 2) {
// value 2 means "The page was accessed by navigating into the history"
console.log('Reloading');
window.location.reload(); // reload whole page
}
2.如果頁面被緩存,則重新加載整個頁面
window.onpageshow = function (event) {
if (event.persisted) {
window.location.reload();
}
};
這個簡單的解決方案貼在這里強制頁面刷新后退按鈕工作...
當用戶點擊后退按鈕時,我試圖強制瀏覽器再次下載頁面,但沒有任何效果。 我看來,現代瀏覽器具有單獨的頁面緩存,它存儲頁面的完整狀態(包括JavaScript生成的DOM元素),因此當用戶按下后退按鈕時,前一頁面立即顯示在用戶離開的狀態。 如果要強制瀏覽器重新加載后退按鈕上的頁面,請將onunload =“”添加到(X)HTML body元素:
<body onunload="">
這會禁用特殊緩存並在用戶按下按鈕時強制頁面重新加載。 使用前請三思而后行。 需要這種解決方案的事實是暗示您的網站導航概念存在缺陷。
你嘗試過這樣的事嗎? 未經測試......
$(document).ready(function(){
$('.ajaxAnchor').on('click', function (event){
event.preventDefault();
var url = $(this).attr('href');
$.get(url, function(data) {
$('section.center').html(data);
var shortened = url.substring(0,url.length - 5);
window.location.hash = shortened;
});
});
});
首先在代碼中插入字段:
<input id="reloadValue" type="hidden" name="reloadValue" value="" />
然后運行jQuery:
<script type="text/javascript">
jQuery(document).ready(function()
{
var d = new Date();
d = d.getTime();
if (jQuery('#reloadValue').val().length === 0)
{
jQuery('#reloadValue').val(d);
jQuery('body').show();
}
else
{
jQuery('#reloadValue').val('');
location.reload();
}
});
您可以通過單擊后退按鈕使用以下內容刷新頁面:
window.addEventListener('popstate', () => {
location.reload();
}, false);
嗯u_u
正如我所說,后退按鈕是我們每個人在某個地方痛苦的......那說...
只要你正常加載頁面就會給它帶來很多麻煩...對於一個標准的“網站”它不會改變那么多......但是我認為你可以做這樣的事情
用戶每次訪問您的頁面.php,選擇要加載的內容。 您可以嘗試使用緩存(不緩存頁面)和可能過期日期。
但是長期解決方案將在“onload”事件上放置一個代碼來獲取Ajax的數據,這樣你就可以(用Javascript)運行你想要的代碼,並刷新頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.