在<body onbeforeunload="alert('提示')"></body>时提示<body>中没有onbeforeunload事件,写onunload可以但是onbeforeunload提示没有这个元素。提示错误:Validation(XHTML1.0 Transitional):Attribute 'onbeforeunload' is not a valid attribute of element'body'.

解决方案 »

  1.   

    window.onbeforeunload = function(){}
      

  2.   

    window.onbeforeunload = "你确定要离开吗?"
      

  3.   

    <html xmlns="http://www.w3.org/1999/xhtml">
       <head>
       <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
       <title>onbeforeunload测试</title>
       <script>
       function checkLeave(){
             event.returnValue="确定离开当前页面吗?";
       }
       </script>
       </head>
       <body onbeforeunload="checkLeave()">
       </body>
       </html>
      

  4.   

    谢谢你的回答,不过我现在的问题是在<body>里onbeforeunload会报错,onunload就没事。我现在是想实现阻止浏览器关闭的功能。可是这样还是无法阻止浏览器关闭。
      

  5.   

    onunload 所有浏览器都支持
    onbeforeunload 部分浏览器支持
      

  6.   

    http://www.cnblogs.com/viter/archive/2010/01/23/1654690.html
      

  7.   

    是浏览器的问题吗?我感觉是我的编程环境的问题,我用的VS2010,在HTML中写入onbeforeunload它就会在下面有绿色错误提示线,也就是说在<body>标签里没有这个元素可用。
    至于浏览器我用的是IE9
      

  8.   

    像这种问题在百度GOOGLE为什么都没有提到过呢