Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Window.onbeforeunload-了解用户单击的内容5_Javascript_Jquery_Angular - Fatal编程技术网

Javascript Window.onbeforeunload-了解用户单击的内容5

Javascript Window.onbeforeunload-了解用户单击的内容5,javascript,jquery,angular,Javascript,Jquery,Angular,我目前正在使用angular 5开发一个应用程序 在用户离开页面之前,我检查他们是否更改了任何表单字段并且没有提交。我有类似的东西 // lunch when closing the page o reload it @HostListener('window:beforeunload', ['event']) beforeUnloadHander(event) { if(!this.accepted && this.details.length != 0){

我目前正在使用angular 5开发一个应用程序

在用户离开页面之前,我检查他们是否更改了任何表单字段并且没有提交。我有类似的东西

// lunch when closing the page o reload it
@HostListener('window:beforeunload', ['event'])
  beforeUnloadHander(event) {
    if(!this.accepted && this.details.length != 0){
    var message = 'Sure you want to leave?';
    if (typeof event == 'undefined') {
      event = window.event;
    }
    if (event) {
      event.returnValue = message;
      console.log(event)
    }
    // console.log(event.returnValue)
    return message;
    }  
我的面板上有这样的东西


现在的问题是,我想知道用户按了什么。如果他按下[离开],则运行功能a;如果他按下[停留],则运行功能b。这似乎不可能实现…

创建自己的消息框并使用promise确定用户按下了什么。

如果您也处理“卸载”事件,您将知道用户选择了“离开”如果调用了该事件处理程序,为什么不使用警报,甚至是确认dailouge,它会有yes和no,当我使用confirm chrome阻止它显示时,你可以捕捉到它,我在控制台上收到消息“blocked confirm…”@Pointy您能提供更多详细信息吗?您想解决什么更高级别的问题?无法使用带有“beforeunload”事件的自定义对话框。@Pointy他建议您只使用两个选项进行提示,然后在用户选择“离开”时手动关闭窗口。@DavidAnthonyAcosta是的,但这与“beforeunload”处理程序。“beforeunload”事件发生在任何关闭窗口的情况下(至少在浏览器的能力范围内)。