Javascript onbeforeunload函数 [英] Javascript onbeforeunload function

查看:40
本文介绍了Javascript onbeforeunload函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在onbeforeunload事件上调用此确认函数,但未调用它.我在这里想念什么?
我在body标签中称呼它:<body onbeforeunload="confirmation();">
我也尝试过:<body onbeforeunload="return confirmation();">

Folks I am trying to call this confirmation function on onbeforeunload event but its not getting called. What am I missing here?
I am calling this inside the body tag: <body onbeforeunload="confirmation();">
I also tried: <body onbeforeunload="return confirmation();">

代码:

  function confirmation(){
  var answer = confirm('You have unsaved changes!!');
   if(answer == true)
    {
     alert('Bye Bye!!!');
    }
    else{
      alert('You are staying');
     return false;
    }
   }

推荐答案

onbeforeunload 有效,并且代码中包含语法错误.

That's not remotely how onbeforeunload works, and the code contains syntax errors.

window.onbeforeunload = function (e)
{
    var e = e || window.event,
        message = 'You have unsaved changes!!';

    // For IE and Firefox prior to version 4
    if (e)
    {
        e.returnValue = message;
    }

    return message;
}

这篇关于Javascript onbeforeunload函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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