jQuery、onBeforeUnload 和 ASP.Net 表单
我有以下解决方案来阻止用户意外离开我的注册页面:
var warning = true;
function CheckExit() {
if (warning) {
return "Changes done. Remember to save, blah blah.";
}
}
window.onbeforeunload = CheckExit;
addToPostBack = function (func) {
var old__doPostBack = __doPostBack;
if (typeof __doPostBack != 'function') {
__doPostBack = func;
} else {
__doPostBack = function (t, a) {
if (func(t, a)) old__doPostBack(t, a);
}
}
};
$(document).ready(function () {
$('#aspnetForm').submit(function () {
warning = false;
return true;
});
addToPostBack(function (t, a) {
warning = false;
return true;
});
});
大部分代码来自 StackOverflow 上的不同问题,并且在我的本地调试页面上运行良好;所有 ASP.Net 控件(链接按钮、按钮)都可以在不发出警告的情况下单击,但一旦用户尝试关闭窗口或导航离开,就会显示警告。
但是,当部署到我的服务器时,当我单击 ASP.Net 控件时也会弹出警告(并且我真的不想警告用户,当他单击“保存”按钮时,他的更改未保存)。现在,我的本地调试页面和服务器之间存在差异:服务器使用不同的 MasterPage,其中包含一些广告,但在使用 Firebug 进行调试后(请参阅下一段),我看不出这会产生什么影响。
我尝试使用 Firebug 运行 Javascript 代码的不同部分,以确认所有内容均已正确加载(我的第一个猜测是包含 $('#aspnetForm') 的部分未加载,因为这似乎是区别) ,但根本没有任何改善。每次单击链接或按钮时仍然会弹出警告。
我认为我的主要问题是我不完全理解 addToPostBack 函数的作用,因此我无法正确调试它。
这里有人知道可能出了什么问题吗?
I have the following solution for stopping the users from accidentally leaving my registration page:
var warning = true;
function CheckExit() {
if (warning) {
return "Changes done. Remember to save, blah blah.";
}
}
window.onbeforeunload = CheckExit;
addToPostBack = function (func) {
var old__doPostBack = __doPostBack;
if (typeof __doPostBack != 'function') {
__doPostBack = func;
} else {
__doPostBack = function (t, a) {
if (func(t, a)) old__doPostBack(t, a);
}
}
};
$(document).ready(function () {
$('#aspnetForm').submit(function () {
warning = false;
return true;
});
addToPostBack(function (t, a) {
warning = false;
return true;
});
});
Most of this code is from different questions here on StackOverflow, and works just fine on my local debugging page; all ASP.Net controls (linkbuttons, buttons) can be clicked without a warning, but as soon as the user tries to close the window or navigate away, the warning is displayed.
However, when deployed to my server, the warning pops up when I click ASP.Net controls too (and I really do not want to warn the user that his changes isn't saved when he clicks the "Save" button). Now, there is a difference between my local debugging page and the server: the server uses a different MasterPage with some ads in it, but after debugging with Firebug (see next paragraph), I can't see how that should make a difference.
I have tried running different parts of the Javascript code by using Firebug to confirm that everything is loaded correctly (my first guess was that the section containing $('#aspnetForm') wasn't being loaded, since this seems to be the difference), but there is no improvement at all. The warning still pops up on every click of a link or a button.
My main problem is, I think, that I do not fully understand what the addToPostBack function does, and as such, I cannot properly debug it.
Is there anybody here who has any idea what might be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个猜测,但你没有传统的防冲突装置
(函数 ($) { 此处代码 })(jQuery);
因此,某些内容可能与 jQuery 的“$”定义相冲突,这会在有人回发时停止执行代码所产生的异常。
It's a guess, but you don't have the conventional anti-conflict device which goes
(function ($) { code here })(jQuery);
so something might be conflicting with jQuery's definition of '$' which would stop the execution of the exception your code makes when someone is posting back.
我同意詹姆斯的观点。大多数时候都会发生这种类型的冲突。我会建议使用 noConflict()。
快乐编码。
I agree with James. Most of the time such type of conflict occurs. I will suggest with noConflict().
Happy Coding.