原文发布时间为:2008-10-20 —— 来源于本人的百度文章 [由搬家工具导入]

onbeforeunload

可以在页面关闭,刷新,跳转时弹出提示信息,防止意外的跳转使得当前页的表单内容被清空。如果用onuload的话会使页面关闭之后才执行事件。所以,还是推荐使用这个onbeforeunload

-----------------------------------
用法:

  • object.onbeforeunload = handler
  • <element onbeforeunload = “handler” … ></element>

描述:
事件触发的时候弹出一个有确定和取消的对话框,确定则离开页面,取消则继续待在本页。
handler可以设一个返回值作为该对话框的显示文本。

触发于:

  • 关闭浏览器窗口
  • 通过地址栏或收藏夹前往其他页面的时候
  • 点击返回,前进,刷新,主页其中一个的时候
  • 点击 一个前往其他页面的url连接的时候
  • 调用以下任意一个事件的时候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.
  • 当用window open打开一个页面,并把本页的window的名字传给要打开的页面的时候。
  • 重新赋予location.href的值的时候。
  • 通过input type=”submit”按钮提交一个具有指定action的表单的时候。

可以用在以下元素:
BODY, FRAMESET, window

平台支持:
IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, Firefox0.9+
三大主流浏览器中firefox和IE都支持onbeforeunload事件,opera尚未支持
-----------------------------------

范例:
1.
<script>
function window.onbeforeunload(){
return"文字提示信息";
}
</script>
2.
<body onbeforeunload="";>

缺陷:
会出现一个确认离开当前页面的提示框,尚不知道如何去除...

onbeforeunload 的使用的更多相关文章

  1. 刷新或关闭时调用onbeforeunload

    Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或者在<body>里指定.区别在于on ...

  2. 浏览器 onbeforeunload 事件 兼容写法

    window.onbeforeunload = onbeforeunload_handler; function onbeforeunload_handler() { var warning = &q ...

  3. 关于onbeforeunload的一些想法

    页面在关闭前会有onbeforeUnload事件,来询问用户是否要关闭这个页面OR选项卡 浏览器的F5刷新为按下F5----onbeforeUnload----onunload----onload; ...

  4. 【原】js离开页面执行函数 onbeforeunload与onunload事件

    在最近的项目中,需要做到一个时间,就是用户离开页面的时候,我需要缓存页面其中一部分的内容,但是我不需要用户刷新的时候也缓存,我只希望在我用户离开的时候 执行这个函数.百度之,有onbeforeunlo ...

  5. 在页面关闭或者刷新的时候触发 onbeforeunload

    该实例演示了如何使用 HTML DOM 向 body 元素添加 "onbeforeunload" 事件. 关闭当前窗口,按下 F5 或点击以下链接触发 onbeforeunload ...

  6. javascript的onbeforeunload函数在IOS上运行

    今天在做项目的时候,组长让我用iPad测试一下前面写的离线缓存,后退不刷新页面,发现在iPad上onbeforeunload函数在iPad上一带而过,不运行??? 无奈之下,发现原来在IOS上,有自己 ...

  7. jQuery之load、unload、onunload和onbeforeunload

    1.load:jQuery load() 方法是简单但强大的 AJAX 方法.load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法:$(selector).load(URL,dat ...

  8. window.onbeforeunload 如果取消, 那么javascript变量会保存

    function confirmQuit1() { if (ischanged) return 'it is changed !! '; else return 'no change .. '; } ...

  9. onbeforeunload、beforeunload

    <script type="text/javascript"> function addOnBeforeUnload(e) {     var ev = e || ev ...

  10. 各浏览器对 onbeforeunload 事件的支持与触发条件实现有差异

    转载:http://www.w3help.org/zh-cn/causes/BX2047 标准参考 无. 问题描述 一般情况下,onbeforeunload 事件处理函数内会写入一些提示性语句,当用户 ...

随机推荐

  1. python 用requests请求,报SSL:CERTIFICATE_VERIFY_FAILED错误

    https://www.aliyun.com/jiaocheng/437481.html

  2. MySQL解决中文编码问题

    转载组员博客 地址:MySQL解决中文编码问题

  3. content is king – Bill Gates (1/3/1996) 内容为王 - 比尔盖茨

    以下中文版本由谷歌翻译 内容为王 - 比尔盖茨(1/3/1996) 内容是我期望在互联网上赚取大部分真钱的地方,就像在广播中一样. 半个世纪前开始的电视革命催生了许多行业,包括制造电视机,但长期的赢家 ...

  4. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

    Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...

  5. Python 文件操作Error: binary mode doesn't take an encoding argument

    Python 报错:ValueError: binary mode doesn't take an encoding argument 在运行文件操作相关功能时报错:ValueError: binar ...

  6. Fibonacci again and again HDU - 1848

    任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)=2; F(n)=F(n-1)+F(n-2)(n>=3); 所以,1, ...

  7. js 实现5秒倒计时后跳转页面

    <script type="text/javascript"> function countDown(secs, surl) { var jumpTo = docume ...

  8. loj2058 「TJOI / HEOI2016」求和

    推柿子 第二类斯特林数的容斥表达 fft卡精度就用ntt吧qwq. #include <iostream> #include <cstdio> using namespace ...

  9. 【4Sum】cpp

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  10. MongoDB快速入门学习笔记8 MongoDB的java驱动操作

    import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import org.bson.D ...