离开页面确认主要是利用了onbeforeunload事件,当该事件声明为:
- <body οnbefοreunlοad="return pageBeforeunload(event);">
该方式具体的代码如下:
- <script type="text/javascript">
- var curElement;
- function pageBeforeunload(evt){
- return 'Are you sure you want to leave this page?';
- }
- </script>
- <body οnbefοreunlοad="return pageBeforeunload(event);">
- </body>
另外一种方式是把onbeforeunload声明为:
- <body οnbefοreunlοad="pageBeforeunload(event);">
- function pageBeforeunload(evt){
- var confirm = confirm('Are you sure to leave this page?');
- if(confirm == true){
- // 添加必要的处理逻辑
- }
- }