在某些链接上禁用浏览器onUnload? [英] Disable Browser onUnload on certain links?

查看:104
本文介绍了在某些链接上禁用浏览器onUnload?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有DNS记录管理器的Web托管面板.我必须使用很多JavaScript才能使其几乎像台式机应用程序一样发挥作用.

I am working on a Web Hosting panel which has a DNS records manager. I have to use a lot of JavaScript to make it perform almost like a desktop application.

我真的很想在浏览器运行beforeunload时显示自定义对话框"窗口,但是经过一番研究,我发现我被浏览器的默认对话框"窗口困住了.

I really wanted to show a Custom Dialog window when a a browser runs beforeunload however after some research I have discovered I am stuck using the browsers default Dialog window.

所以下面的JavaScript代码就是这样做的,我的问题是我需要确保它不会在某些链接点击时被触发.

So this JavaScript code below does just that, my problem is I need to make sure it does NOT get fired off on certain link clicks.

我希望能够维护一个CSS类名称数组,并且如果单击的链接在此数组中,则它将不会显示onunload事件.

I am hoping to be able to maintain an Array of CSS class names, and if the link clicked is in this array then it will NOT show the onunload event.

我相信这对于JavaScript开发人员来说很容易.有人可以告诉我该怎么做吗?

I'm sure this is easy for a JavaScript developer. Could someone show me how to do this?

//sample Class array
safeLinks = ['test', 'test2', 'test3', 'test4', 'test5'];

// onunload event
$(window).on('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

推荐答案

您可以在单击事件处理程序时将其删除:

You could remove the event handler when they're clicked:

var safeLinks = ['.test', '.test2', '.test3', '.test4', '.test5'];

function promptBeforeClose() {
    return 'Are you sure you want to leave?';
}

$(window).on('beforeunload', promptBeforeClose);

$(document).on('click', safeLinks.join(', '), function(e) {
    $(window).off('beforeunload', promptBeforeClose);
});

此外,如果这些安全链接具有共同的祖先,请使用该祖先而不是document,因为委托父母或其他人会抱怨. ;)

Also, if these safe links have a common ancestor, use that instead of document as the delegation parent or people will complain. ;)

这篇关于在某些链接上禁用浏览器onUnload?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆