这里有新鲜出炉的 Javascript 教程,程序狗速度看过来!
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
BS 系统一个非常烦人的地方是不知道用户什么时候关闭浏览器离开系统. 这里, 我们采用 onbeforeunload 参数监控用户是否关闭浏览器.
[Ctrl+A 全选 注: 如需引入外部 Js 需刷新才能执行]
- <html>
- <head>
- <title>
- test
- </title>
- </head>
- <body onbeforeunload="RunOnBeforeUnload()">
- <script language="javascript">
- function RunOnBeforeUnload() {
- window.event.returnValue = '关闭浏览器将退出系统.';
- }
- </script>
- </body>
- </html>
效果如下所示:
当 wondow.event.returnValue 为空的时候, 这个选择提示框是不会出现的.
可是问题又来了: 假如我希望用户点击 "确定" 按钮关闭浏览器的时候能够保存用户相关数据. 那么我又应该怎么做呢?
看来只有用 onunload 参数了.
将上面的代码修改如下:
- <html>
- <head>
- <title>
- test
- </title>
- </head>
- <body onbeforeunload="RunOnBeforeUnload()" onunload="RunOnUnload()">
- <script language="javascript">
- function RunOnBeforeUnload() {
- window.event.returnValue = '关闭浏览器将退出系统.';
- }
- function RunOnUnload() {
- //执行你的代码
- }
- </script>
- </body>
- </html>
来源: http://www.phperz.com/article/17/0709/286753.html