【发布时间】:2014-03-30 14:47:13
【问题描述】:
我们的 Sharepoint Web 应用程序中的所有页面都使用表单,并且我们要求在用户到达页面时必须将表单重置(恢复为默认值)。我们已经有了在页面首次加载时运行的代码(它运行if(!Page.IsPostback)),它使用必要的默认值填充表单。
但是,当使用后退按钮时,此行为会被破坏。表单保留上次访问页面时使用的值。我相信这是因为后退按钮不会重新加载页面,而只是将其从缓存中恢复。
建议的解决方案是让后退按钮强制重新加载前一页的 URL。这是我的代码:
//$(window).unload(function () { //does not work in Firefox
$(window).bind('beforeunload', function () {
// when the back button is hit, obtain the
// URL of the prev. page (document.referrer)
// and manually navigate to it forcing the page to reload
try {
if (document.referrer) {
window.event.returnValue = false;
window.location = document.referrer + "?q=" + $.now();
}
}
catch (e) {
// unload will also be triggered during a postback,
// at which time an "Access Denied" error will be
// thrown for the usage of window.location. This error
// can be ignored, since this is the right behaviour.
}
});
此代码未按预期工作。
-
document.referrer并不总是在 IE 中工作,并返回 NULL 通常,而不是上一页的 URL。 - 在 IE9 中,分配给
window.location会引发“未指定错误”。 -
window.unload在页面刷新时也会触发, 选项卡已关闭,或使用了书签链接等,此时 上面的代码不应该被执行。
我正在寻找一种万无一失的方法:
- 将卸载期间的后退按钮与其他卸载技术区分开来。
- 获取上一页的 URL
【问题讨论】:
标签: javascript jquery