- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个企业应用程序,其中表单是动态生成的,我们无法控制代码。表单中的大多数控件都具有与其关联的以下代码
<select name="s_10_1_1_0" onchange = 'SWESubmitForm(document.SWEForm10_0,s_5,"","1-E0RHB7")' id='s_10_1_1_0' tabindex=1997 >
<input type="text" name='s_10_1_11_0' value='' onchange = 'SWESubmitForm(document.SWEForm10_0,s_7,"","1-E0RHB7")' id='s_10_1_11_0' tabindex=1997 maxlength="30">
当用户尝试使用页眉或页脚中的链接导航到其他页面时,我们希望显示一个确定取消对话框。
我习惯于按照以下代码绑定(bind) onbeforeunload 事件并向用户呈现一个确定取消对话框。
//this identifies the unique page name
var viewName = $('input[name="SWEView"]').val();
// if I am on desired page bind the event so that now dialog is presented to user
if(viewName == "XRX eCustomer Create Service Request View"){
$(window).bind('beforeunload', function(){
return 'Your Inquiry has not been submitted yet.';
});
}
但问题是,每次用户更改表单中任何字段的值时,都会出现此对话框(我相信这是由于 onchange 事件中存在 SWESubmitForm 代码所致)。
如果用户单击表单之外的任何其他链接,或者换句话说,将 onbeforeunload 与页面上的选择性事件(包括关闭窗口)联系起来,我希望出现 onbeforeunload 对话框。
请帮忙
最佳答案
.onbeforeunload()
是全局的。您不能仅针对某些离开页面的方式触发它。如果安装了事件处理程序,无论查看者如何离开页面,它都会触发。
您可以跟踪自己的内部状态,并决定是在 onbeforeunload()
处理程序中提示任何内容,还是不返回任何内容(因此不进行提示)。
所以,你可以有这样的代码:
//this identifies the unique page name
var viewName = $('input[name="SWEView"]').val();
// if I am on desired page bind the event so that now dialog is presented to user
if(viewName == "XRX eCustomer Create Service Request View"){
$(window).bind('beforeunload', function() {
// figure out whether you need to prompt or not
if (myStateSaysToPrompt) {
return 'Your Inquiry has not been submitted yet.';
} else {
return; // no prompt
}
});
}
更新
由于这对于谷歌获得很多浏览量来说相当高,因此值得注意的是,这个答案不再有效。
$(window).bind('beforeunload', function() {
请注意,所绑定(bind)的事件中不再有“on”。在更高版本的 jQuery 中使用之前的 onbeforeunload
将导致什么也不会发生。
关于jquery - onbeforeunload - 绑定(bind)到选择性事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882502/
这是我用于window.onbeforeunload的代码 window.onbeforeunload = func; function func() {
我对我的申请有疑问。每当用户意外关闭浏览器窗口时,我想在此之前执行一些清理操作。我已经使用了 onunload 事件,但问题是该事件有时会触发,有时不会。我该怎么办,有没有更好的方法来处理此类问题。
这是我用于window.onbeforeunload的代码 window.onbeforeunload = func; function func() {
我正在尝试开发一个带有单独设置窗口( Electron )的应用程序。当设置窗口打开而主窗口关闭时,我想防止主窗口关闭并聚焦设置窗口。 现在我这样做如下: window.onbeforeunload
我对 JavaScript 还很陌生。我有一个简单的 onbeforeunload 函数,当前适用于所有页面,我希望它为某些子页面(#clients、#products、#news)激活,但不适用于我
我正在使用以下代码。但由于事件未定义,我得到了结果。造成这种情况的可能原因是什么? window.onbeforeunload = function(event){ if(window.e
我想要实现的是,如果用户尝试关闭页面或在不先保存的情况下导航离开页面,则警告用户未保存的更改。 我已经设法弹出 OnBeforeUnload() 对话框...但如果用户没有修改任何字段值,我根本不希望
我有一个 onbeforeunload 事件,该事件应该在用户访问新页面时触发。它工作得很好,但我发现每当用户从他们所在的页面下载文件时,它也会在 Chrome 中触发。 我希望能够判断该事件是否因为
我想检查当前浏览器是否支持 onbeforeunload 事件。常见的 javascript 方法似乎不起作用: if (window.onbeforeunload) { alert('yes
我现在有这个小脚本,用于检查表单上是否进行了任何更改。如果有变化然后我将标志设置为“Y”。我在 body 标记中的 onBeforeunload="changeConfirm()"位置调用此函数。我知
在 MVC 应用程序中离开页面之前,我使用以下 JS 进行确认。 window.onbeforeunload = function() { return 'Any Unsaved data w
这是一个最小的例子 - index.php hi there var id = ""; script.js $(document).read
我有这个代码: (function() { window.onbeforeunload = function () { alert("hi") co
我使用这个片段已经快两年了。那时还好,直到我看到它今天在 Google Chrome 版本 51.0.2704.103m 上无法正常工作。 $(window).on('beforeunload', f
2007 年,在一篇名为“Mastering The Back Button With Javascript”的帖子中,Patrick Hunlock 声称只包含一个 onbeforeunload页面
我们有一些用户填写的表格。如果在此过程中导航离开,我需要执行操作。 我发现了许多在浏览器中组合 onbeforeunload 和 unload 的示例,以提示用户并提醒他们,如果他们关闭或导航离开信息
如何禁用链接的 onbeforeunload? var show = true; function showWindow(){ if(show){ alert('Hi
我正在通过以下代码跟踪窗口关闭/导航事件。 window.onbeforeunload = winClose; function winClose() { if (isData
我有 Uber-Upload! 我想做的是,如果你按下那个按钮,它也会设置一个变量,这样如果你试图离开页面,它会弹出其中一个“你确定要离开吗此页面”警报,以防止人们在上传过程中意外离开。 不要担
我想在用户离开网站时更新数据库。我正在为我的网站使用 php。我正在使用 javascript 函数 onbeforeunload 并通过 ajax 调用 php 来更新数据库。 现在的问题是 Ope
我是一名优秀的程序员,十分优秀!