【发布时间】:2013-01-27 08:18:28
【问题描述】:
您好,我有一张大表要管理。当我单击按钮时,我使用
$.post("update_the_database.php", { mode: 'themode', username: username },
function(result){
window.location.href = window.location.href;
});
这会在后台运行"update_the_database.php",然后在完成后重新加载页面。它会回到页面顶部...
我试过了:
window.location.href = window.location.href + '#the_id';
但在所有浏览器(IE、Firefox、Chrome、Safari)中,它会更改地址栏中的 URL,但不会重新加载页面。
从这里开始:
Difference between window.location.href=window.location.href and window.location.reload()
“window.location.href=window.location.href 如果 URL 中有锚点 (#) 将不会重新加载页面 - 在这种情况下您必须使用 window.location.reload()。”
所以我尝试了:
window.location.reload(true);
它会重新加载页面并向下滚动到 Chrome 和 Safari 中的上一个位置,而不是 IE 和 Firefox...
How to force a page to reload if all what was changed in url is hash?
方法 #2:
window.location.hash = "#" + newhash;
window.location.reload(true);
现在它可以在 Firefox 中运行...
顺便说一句,如果我使用 window.location.reload(true);在 IE 中会出现:
Windows Internet Explorer
To display the webpage again, the web browser needs to
resend the information you've previously submitted.
If you were making a purchase, you should click Cancel to
avoid a duplicate transaction. Otherwise, click Retry to display
the webpage again.
Retry Cancel
您必须按“重试”,否则会出现错误页面,显示“网页已过期”。
为了避免我目前正在做的 IE 弹出窗口:
window.location.hash = "#<?php echo $id;?>";
if (navigator.appName != 'Microsoft Internet Explorer') {
window.location.reload(true);
}
虽然它只是将哈希值放入地址栏中,并且即使我转到地址栏并按 Enter 也不会刷新页面。我必须删除哈希并按 Enter 重新加载页面...(然后滚动到顶部)
【问题讨论】:
标签: javascript