简体   繁体   English

如何禁用浏览器功能

[英]How to disable browser feature

I am developing a project where user gets a conformation page. 我正在开发一个项目,用户可以在其中获得一个构想页面。 I want user not to click back or close tab or reload. 我希望用户不要单击返回或关闭选项卡或重新加载。 Now either I need to disable the browser features or get back button,tab close event, or reload event to java script so that I could take the needed steps to prevent my data to get lost. 现在,我需要禁用浏览器功能或返回按钮,关闭选项卡事件或将事件重新加载到Java脚本,以便可以采取必要的步骤来防止数据丢失。 I have used this: 我用了这个:

window.onbeforeunload = function() 
{
     return "Try This"; 
};

But this get called even when I click a button that redirects the page. 但这即使在我单击重定向页面的按钮时也会被调用。

If you just want to have the alert, understanding that the user is ultimately in control and can bypass your alert, then do what you're doing but use a flag that disables it when you're navigating and don't want the alert. 如果您只想接收警报,请了解用户最终将控制住并可以绕过警报,然后执行您正在做的事情,但是使用一个在导航且不希望接收警报时将其禁用的标志。 Eg: 例如:

var warnWhenLeaving = true;
window.onbeforeunload = function() {
    if (warnWhenLeaving) {
        return "your message here";
    }
};

then in a click handler on the link/button/whatever that moves the user on that you don't want this to pop up on: 然后在点击处理程序上的链接/按钮/将用户移动到的所有内容(不希望其弹出):

warnWhenLeaving = false;

In a comment you asked: 在评论中,您询问:

can i know that what user has clicked when alert is generated with this function. 我可以知道使用此功能生成警报时用户单击了什么。 That is can i know what user has clicked (leave this page/stay on page) 那可以知道用户单击了什么(离开此页面/留在页面上)

The answer is: Sort of , but not really; 答案是: 有点 ,但不是真的。 you're almost certainly better off not trying to. 几乎可以肯定,最好不要尝试。

But: If you see your onbeforeunload function run, then you know the user is leaving the page and the browser is likely to show them your message. 但是:如果您看到onbeforeunload函数正在运行,那么您就知道用户正在离开页面,浏览器可能会向他们显示您的消息。 The browsers I'm familiar with handle the popup like an alert : All JavaScript code on the page is blocked while the popup is there. 我熟悉的浏览器会像alert一样处理弹出窗口:弹出窗口在那里时,页面上的所有JavaScript代码都会被阻止。 So if you schedule a callback via setTimeout , you won't get the callback if they leave and you will if they stay: 因此,如果您通过setTimeout安排回调,则当它们离开时将不会得到该回调,而如果它们停留则将得到该回调:

window.onbeforeunload = function() {
  if (warnWhenLeaving) {
    setTimeout(function() {
      display("You stayed, yay!");
    }, 0);
    return "No, don't go!";
  }
};

Live Example 现场例子

So in theory, if you get the callback, they stayed; 因此,从理论上讲,如果您获得回调,则它们会保留下来; if you see an unload event, they left. 如果看到unload事件,他们就离开了。 (Note that there are very few things you can do in an unload event.) (请注意,有少数的事情,你可以在做unload事件。)

I've tried that on current Chrome, current Firefox, IE8, and IE11: It works on all of those. 我已经在当前的Chrome,当前的Firefox,IE8和IE11上进行过尝试:它适用于所有这些。 Whether it will work in the next release of any of them is anybody's guess. 任何人都怀疑它是否将在其中任何一个的下一个版本中起作用。 Whether it works reliably on mobile browsers is something you'd have to test, and again could change. 它是否必须在移动浏览器上可靠运行是您必须测试的问题,并且可能会再次发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM