阻止用户关闭网页,提示保存的解决方案IE/FF/OP通用(未经测试)
2009-08-25 20:08
369 查看
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 (form’s 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.
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 (form’s 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.
相关文章推荐
- 阻止用户关闭网页,提示保存的解决方案IE/FF/OP通用(未经测试)
- 阻止用户关闭网页,提示保存的解决方案IE/FF/OP通用(stop page from closing, and remind user save unchanged data )
- 网页窗口关闭前进行提示(适用于IE和FF, 标题栏的X关闭按钮)
- 网页关闭时提示用户保存数据
- 网页窗口关闭前进行提示(适用于IE和FF, 标题栏的X关闭按钮)
- 浏览器兼容性问题解决方案之CSS——已在IE、FF、Chrome测试
- Select 可编辑 - 完美支持各大主流浏览器 最近做项目有个select可编辑的需求,一时棘手,网上找了很多解决方案都不完美,没办法自己写了一个,经测试IE,FF,chrome都支持。特此拿出来
- 怎么解决内存不足以及IE弹出提示:此网页上的问题导致internet explorer关闭并重试
- 目前最佳解决方案:关闭浏览器前提示用户确认是否关闭
- [举一反三]解决IE下select标签innerHTML插入option的BUG(兼容IE,FF,Opera,Chrome,Safari) 前言: 这是一个老bug了,现在提供一个完美解决方案。由于我一直是用createElement来创建动态的op
- IE 11打开之后一直提示“恢复非正常关闭的网页”,让后IE就卡死
- 不会提示是否关闭浏览器 IE6、IE7、FF通用代码
- 浏览器兼容性问题解决方案之CSS——已在IE、FF、Chrome测试
- 当root用户无密码,非超级权限用户时提示mysqladmin: Can't turn off logging; error: 'Access denied; you need the SUPER privilege for this op解决方案
- 浏览器兼容性问题解决方案之CSS——已在IE、FF、Chrome测试
- 点击网页中PDF链接时,打开PDF文档不再是直接IE查看,而是提示下载保存
- 【SQLServer】【恢复挂起的解决方案】附加文件时候的提示“无法重新生成日志,原因是数据库关闭时存在打开的事务/用户,该数据库没有检查点或者该数据库是只读的。 ”【数据库恢复】
- IE用户google搜索提示Internet Explorer 无法显示该网页的解决办法
- 一个网页通用的测试用例(借鉴他人的保存,加注释)
- 一个网页通用的测试用例(借鉴他人的保存,加注释)