zoukankan      html  css  js  c++  java
  • 阻止用户关闭网页,提示保存的解决方案IE/FF/OP通用(未经测试)

    in fact the onbeforeunload event supported in Firefox browser, but the window.event is not supported in Firefox.
     
    So, we can use the following code to stop page from leaving:
     
        function SaveRemind()
        {
            window.event.returnValue= "input has not been saved, are you sure to leave?";   
    }
     
    But we should use the following code in Firefox to achieve the same effect:
     
        function SaveRemind()
        {
            return "input has not been saved, are you sure to leave?";   
        }
     
    If you would like to save the user changes in onbeforeunload event, and don’t stop the page from leaving, we can use “window.confirm” function just like following code:
     
                if(window.confirm("Are you want to save data?"))
                {
                     document.getElementById("BtnSave").click();//you can write other codes for save data in this line, just like a AJAX call function.
                     window.alert("data has been saved");
                }
     
    In your scenario, we also need to distinguish whether a post is back occurring or user leaving this page. Please check the following code in italic:
     
    The full code of my test demo (work in Internet Explorer 6.0/7.0 and Firefox 3.0):
     
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="Default11" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript">
        var NeedAlert=true;//when postback occur (forms onsubmit), needn''t alert.
        function SaveRemind()
        {
            if(NeedAlert)
            {
                if(window.confirm("are you want to save data?"))
                {
                     document.getElementById("BtnSave").click();//you can write other codes for save data in this line.
                     window.alert("data has been saved");
                }
            }
        }
        </script>
     
    </head>
    <body onbeforeunload="SaveRemind();">
        <form id="form1" runat="server" onsubmit= "NeedAlert=false;" >
        <div>
        </div>
        <asp:Button ID="BtnSave" runat="server" onclick="Button1_Click" Text="BtnSave" />
        </form>
    </body>
    </html>
     
        protected void Button1_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(@"Data Source=....."))
            {
                using (SqlCommand cmd = new SqlCommand("insert into Table1 values(''myname'',''myid'')", conn))
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }
     
    One thing I need to mention is that, I am not sure about if user click “OK” button so quickly and the BtnSave.Click () can also make the data save in database. So, you’d better to write an asynchronous function call, just like AJAX to save the data.
     
    If I’ve misunderstood the facing problem, please feel free to let me know.
  • 相关阅读:
    你不知道的空格
    导致你的微服务走向失败的11个原因
    阿里巴巴正式开源 Inclavare Containers 技术
    如何分辨区块链传销项目?
    编写干净的 React Components & JSX
    面试:3年工作经验程序员应有的技能
    数据库设计规范化的 5 个要求
    mac下镜像飞速安装Homebrew教程
    对优秀程序员的思考
    程序员理想中的工作环境
  • 原文地址:https://www.cnblogs.com/future/p/1274216.html
Copyright © 2011-2022 走看看