使用浏览器后退按钮时如何禁用正文加载

问题描述 投票:0回答:1

我们使用body onload函数初始化表单上的选择如果用户修改了这些选择并提交表单,则通常需要单击以更改选择并再次提交表单。

当用户单击时,表单显然会从重载功能中重置。我们希望用户能够使用“后退”按钮查看他们选择的选项。

我们已经尝试了几种变量方法来运行一次并在Submit按钮中禁用onload。没有成功。

有什么想法吗?

onload back-button disable
1个回答
0
投票
尝试在清除表单之前从onload函数调用此方法:

function isBackForward() { if (typeof performance === "object") { // Use the newer API if available. // https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming if (typeof performance.getEntriesByType === "function") { var entries = performance.getEntriesByType("navigation"); if (entries.length === 1) { if (entries[0].type === "back_forward") { return true; } else { return false; } } else { // unable to determine return void(0); // undefined } // Try the older deprecated API instead // https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigation } else if (typeof performance.navigation === "object") { if (performance.navigation.type === performance.navigation.TYPE_BACK_FORWARD) { return true; } else { return false; } } else { // unable to determine return void(0); // undefined } } else { // unable to determine return void(0); // undefined } }

如果使用后退或前进,此函数将返回true;如果浏览器不支持所需的API来确定,则返回undefined;如果是另一种导航类型,则返回false。

遗憾的是,仅此一项无法在Safari中使用。对于Safari,您可以在按下“提交”按钮时在sessionStorage中为页面设置状态值(请参见https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage),然后在清除表单之前检查状态值。

© www.soinside.com 2019 - 2024. All rights reserved.