jQuery仅在关闭窗口时显示弹出窗口

编程入门 行业动态 更新时间:2024-10-10 03:27:23
本文介绍了jQuery仅在关闭窗口时显示弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在用户关闭浏览器/选项卡时显示一条确认消息,而在单击页面上的任何链接时不显示该消息.

I am trying to display a confirmation message when the user closes the browser/tab and not when any of the links on page is clicked.

我已经在窗口关闭上显示消息的第一部分工作正常,但是问题是当用户单击页面上的任何链接时,也会显示消息/警报.

I have got the first part of displaying the message on window close working nicely but problem is that the message/alert is displayed also when user clicks on any link on the page.

我从某处获得了代码来防止这种行为,但是无论何时单击任何链接,都会弹出警报.

I have got code from somewhere to prevent this behavior but still when ever any link is clicked the alert pops up.

这是代码:

<script src="js/jquery-1.7.1.min.js"></script> <script src="js/jquery-ui.min.js"></script> <script type="text/javascript" src="js/backfix-min.js"></script> $(document).ready(function() { $(window).bind('beforeunload', function(e){ $("#lead-gen-modal").dialog("open"); // This line only appears in alert boxes for IE return "Wait\! Don\'t Go\! We have a special offer for you\. Stay on this page to receive big savings\!"; }); $("a").click(function() { window.onbeforeunload=null; }); });

推荐答案

只需使用全局变量 并在单击链接时将其设置为false.

just use a global variable and set it to false when clicking a link.

var key = true; $(window).bind('beforeunload', function(e){ if(key) $("#lead-gen-modal").dialog("open"); });

更新:

$(document).on("click","a",function() { key=false; });

或者如果您只想防止关闭窗口,则可以执行以下操作:

or if you just want to prevent closing window you can do this :

window.onbeforeunload = function(e){ if(key) return false; }

更多推荐

jQuery仅在关闭窗口时显示弹出窗口

本文发布于:2023-08-06 11:24:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1311960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关闭窗口   弹出窗口   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!