pra*_*eep 21 javascript jquery
我有一个聊天应用程序,我希望每当用户意外或手动关闭浏览器时,他都应该收到警报,以便可以执行各种清理操作.我有更多的东西,我希望在事件之前使用,但我希望它用于特定的网页,因为所有其他网页也在加载前调用.请帮忙
Ari*_*sky 23
这不是一个真正的JQuery事情,我使用这个函数:
function setConfirmUnload(on) {
window.onbeforeunload = (on) ? unloadMessage : null;
}
function unloadMessage() {
return "Are you sure you want to leave this page";
}
Run Code Online (Sandbox Code Playgroud)
然后,在任何时候你都可以打电话
setConfirmUnload(true)启用确认,如果要允许它们在没有警告的情况下关闭屏幕(例如,如果有关闭按钮),则为false.
Rag*_*hav 23
我们应该阻止或提示用户在执行这些操作时他会丢失他的数据.
为了解释我已经使用了两个文本框,一个带有id TestButton的Asp.net提交按钮.我采取的其他回发控件是另一个asp.net按钮,一个带有autopostback属性为true的复选框,一个带有autopostback属性为true的下拉列表.现在我已经使用了所有这些回发控件,以便向您显示当用户对这些控件执行操作时,我们还需要向用户显示有关数据丢失的promt.在提交按钮上,我们不会显示提示,因为我们必须使用该控制动作提交数据.这是示例代码.我已经在控件更改时设置了window.onbeforeunload方法.
<html >
<head id="Head1">
<title></title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function()
{
// Prevent accidental navigation away
$(':input').bind(
'change', function() { setConfirmUnload(true); });
$('.noprompt-required').click(
function() { setConfirmUnload(false); });
function setConfirmUnload(on)
{
window.onbeforeunload = on ? unloadMessage : null;
}
function unloadMessage()
{
return ('You have entered new data on this page. ' +
'If you navigate away from this page without ' +
'first saving your data, the changes will be lost.');
}
window.onerror = UnspecifiedErrorHandler;
function UnspecifiedErrorHandler()
{
return true;
}
});
</script>
</head>
<body>
<form id="form1" name="myForm" runat="server">
<div>
First Name :<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
Last Name :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<br />
IsMarried :<asp:CheckBox ID="CheckBox1" runat="server" /><br />
<br />
<asp:Button runat="server" ID="TestButton" Text="Submit" CssClass="noprompt-required" /><br />
<br />
<br />
<br />
<br />
<asp:Button runat="server" ID="AnotherPostbackButton" Text="AnotherPostbackButton"
/><br />
<br />
<asp:CheckBox runat="server" ID="CheckboxWhichCausePostback" Text="CheckboxWhichCausePostback"
AutoPostBack="true" /><br />
<br />
DropdownWhichCausePostback<asp:DropDownList runat="server" ID="DropdownWhichCausePostback"
AutoPostBack="true">
<asp:ListItem Text="Text1"></asp:ListItem>
<asp:ListItem Text="Text2"></asp:ListItem>
</asp:DropDownList>
<br />
<br />
</div>
</form>
</body>
Run Code Online (Sandbox Code Playgroud)
以上我用过:
$('.noprompt-required').click(function() { setConfirmUnload(false); });
Run Code Online (Sandbox Code Playgroud)
我在这一行中所做的是我调用setConfirmUnload方法并将false作为参数传递,将其设置window.onbeforeunload为null.那么这意味着在任何你想要不应该提示用户的控件上,给那个控件提供类.noprompt-required并保持其他原样.
您可以尝试卸载事件:
$(window).unload( function () { alert("Bye now!"); } );
Run Code Online (Sandbox Code Playgroud)
http://docs.jquery.com/Events/unload
我猜这次清理只在客户端上进行.了解这些"清理"的性质会很有趣.
| 归档时间: |
|
| 查看次数: |
31515 次 |
| 最近记录: |