使用 Microsoft Edge 记住后退按钮上的表单数据

Remember form data on back button with Microsoft Edge

我使用 ASP.NET MVC(非核心)创建了一个包含搜索和结果页面的 Web 应用程序。

每当我使用 Chrome 和 Firefox 等其他浏览器上的后退按钮返回结果页面时,它会记住文本和下拉值信息。

但在 Microsoft Edge 上,它会将我所有的文本框和下拉菜单清除为默认值。这是浏览器问题还是错误?我们可以通过代码做些什么吗?

我发现了一些类似的东西,但它没有解决这个确切的行为。 Related question

我在旧版 Microsoft Edge (Microsoft Edge 44.18362.449.0) 中重现了该问题,但在 New Microsoft Edge (chromium based) it works well. It can be possible that it is the default behavior in the Legacy version of the Edge. You could feedback this issue in Microsoft Edge Legacy Forum 或安装新版 Microsoft Edge 并使用它。

作为解决方法,您可以尝试使用 WebStorage 来存储表单数据,当页面重新加载时,检查存储是否包含该值,然后设置该值。请检查以下示例:

JavaScript代码:

<script>
    window.onload = function () {
        if (sessionStorage.name && sessionStorage.password) {
            document.getElementById("name").value = sessionStorage.name;
            document.getElementById("password").value = sessionStorage.password;
        }
    };

    function storedata() {
        if (typeof (Storage) !== "undefined") {
            var name = document.getElementById("name").value;
            var password = document.getElementById("password").value;

            sessionStorage.name = name;
            sessionStorage.password = password;

            document.getElementById("res").innerHTML = "Your datas restored";
        } else {
            document.getElementById("res").innerHTML = "Sorry, your browser does not support web storage...";
        }
    } 
</script>

网页:

<form id="form1" runat="server" autocomplete="on">
    <div>
        UserID: <asp:TextBox ID="name" runat="server"></asp:TextBox><br />
        Password: <asp:TextBox ID="password" runat="server"></asp:TextBox><br />
       Remember Me: <asp:CheckBox ID="selectall" runat="server" />  <br />
        <asp:Button ID="btn_signIn" runat="server" Text="Button" OnClick="btn_signIn_Click" OnClientClick="storedata();" /><br />
        <asp:Label ID="lblresult" runat="server" Text=""></asp:Label>
        <span id="res"></span>
    </div>
</form>

有关使用 WebStorage 的更多详细信息,请查看以下链接:

Web Storage API

HTML5 Web Storage