简体   繁体   English

在浏览器关闭之前提示用户?

[英]Prompt User before browser close?

We have an administrative portal that our teachers constantly forget to download their latest PDF instructions before logging out and/or closing the browser window.我们有一个管理门户,我们的教师经常忘记在注销和/或关闭浏览器窗口之前下载他们最新的 PDF 说明。 I have looked around but can't find what I'm looking for.我环顾四周,但找不到我要找的东西。

I want to accomplish the following goals:我想实现以下目标:

Goal 1目标 1

Before a user can close the browser window, they are prompted "Did you remember to download your form?"在用户关闭浏览器窗口之前,系统会提示他们“您记得下载表单吗?” with two options, yes/no.有两个选项,是/否。 If yes, close, if no, return to page.如果是,关闭,如果不是,返回页面。

Goal 2目标 2

Before a user can click the 'logout' button, they are prompted with the same as above.在用户单击“注销”按钮之前,他们会收到与上述相同的提示。

My first pass at the very basic code (which does not work for browser close) is:我对非常基本的代码(不适用于浏览器关闭)的第一遍是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">
function init() {
    if (window.addEventListener) {
        window.addEventListener("beforeunload", unloadMess, false);
    } else if (window.onbeforeunload) {
        window.onbeforeunload = unloadMess;
    };
}

function unloadMess() {
    var User_Message = "[Your user message here]"
    return User_Message;
}
</script>
</head>

<body onload="init();">
     hello this is my site
</body>
</html>

EDIT - NEW ISSUES编辑 - 新问题

The solution provided below works but also has its own issues:下面提供的解决方案有效,但也有其自身的问题:

When user clicks the link to actually download the forms, the page thinks they are trying to leave and in turn present an error message to them!当用户单击链接以实际下载表单时,该页面认为他们正试图离开并反过来向他们显示错误消息! Also - I would like for one event or another to occur but not necessarily both.另外 - 我希望一个事件或另一个事件发生,但不一定同时发生。 Ie they hit 'logout' button and they are presented with the OnClick event, THEN the browser prompt.即他们点击“注销”按钮,然后他们会看到 OnClick 事件,然后是浏览器提示。 Any ideas?有任何想法吗?

Update 2017 2017 年更新

All modern browsers do not support custom messages any longer.所有现代浏览器不再支持自定义消息。

window.onbeforeunload = function(evt) {
   return true;
}

This one for closing the tab:这是用于关闭选项卡的:

window.onbeforeunload = function(evt) {
    var message = 'Did you remember to download your form?';
    if (typeof evt == 'undefined') {
        evt = window.event;
    }
    if (evt) {
        evt.returnValue = message;
    }

    return message;
}

and use onClick event for logout button:并使用onClick事件作为注销按钮:

onClick="return confirm('Did you remember to download your form?');"

I think that this may be part of your problem:我认为这可能是您问题的一部分:

else if(window.onbeforeunload) {
  window.onbeforeunload = unloadMess;
};

That test in the "if" statement will only be true if there's already a handler function. “if”语句中的测试只有在已经存在处理函数时才会为真。 That test doesn't mean, "does the window object have an 'onbeforeunload' property?".该测试并不意味着“窗口对象是否具有 'onbeforeunload' 属性?”。 It means, "is the 'onbeforeunload' property of the window currently not null?".这意味着,“当前窗口的 'onbeforeunload' 属性是否为空?”。

I think it'd be safe to just set "onbeforeunload" directly, for any browser.我认为直接为任何浏览器设置“onbeforeunload”是安全的。

You cannot alert or things like that in onbeforeunload , you cannot simply return false to make the user not leave the page, as with other events like onclick .您不能在onbeforeunload发出警报或类似的事情,也不能像onclick等其他事件一样简单地返回 false 以使用户不离开页面。 This would allow a site to make it impossible to leave it.这将使网站无法离开它。

You can however just return a string, the browser then shows a confirm dialog including your string asking the user whether they really want to leave.但是,您可以只返回一个字符串,然后浏览器会显示一个确认对话框,其中包含您的字符串,询问用户是否真的想离开。

Pointy is right about browser close events - imagine if evil sites could prevent you from closing their windows?? Pointy 关于浏览器关闭事件是正确的 - 想象一下,如果邪恶的网站可以阻止你关闭他们的窗口? So you cannot prevent them from closing your site, but you can do an alert.因此,您无法阻止他们关闭您的网站,但您可以发出警报。

As far as your logout button is concerned, that is much more straight-forward:就您的注销按钮而言,这更加直接:

<a href="logout.html" onClick="return confirm('Did you remember to download your form?');">Logout</a>

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

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