onbeforeunload與onunload事件異同點總結

來源:互聯網
上載者:User

1)異同點
相同點:onunload,onbeforeunload都是在重新整理或關閉時調用,可以在<script>指令碼中通過window.onunload來指定或者在<body>裡指定。
不同點:
a)onbeforeunload在onunload之前執行 ,它還可以阻止onunload的執行。
b)onbeforeunload是正要去伺服器讀取新的頁面時調用,此時還沒開始讀取;nunload則已經從伺服器上讀到了需要載入的新的頁面,在即將替換掉當前頁面時調用 。
c)onbeforeunload是可以阻止頁面的更新和關閉的。但是onunload不能。
onload、onbeforeunload、onunload事件的執行順序:
開啟頁面時:只執行onload
關閉頁面時:先onbeforeunload,後onunload
重新整理頁面時:先onbeforeunload,後onunload,再onload

2)onbeforeunload、onunload事件
觸發於:
a)關閉瀏覽器視窗
b)通過地址欄或收藏夾前往其他頁面的時候
c)點擊返回,前進,重新整理,首頁其中一個的時候
d)點擊 一個前往其他頁面的url串連的時候
e)調用以下任意一個事件的時候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.
f)當用window open開啟一個頁面,並把本頁的window的名字傳給要開啟的頁面的時候。
g)重新賦予location.href的值的時候。
h)通過input type=”submit”按鈕提交一個具有指定action的表單的時候。
可以用在以下元素:
BODY, FRAMESET, window
瀏覽器支援:
IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, Firefox0.9+

3)解決重新整理頁面時不調用onbeforeunload

4)onbeforeunload阻止頁面的更新和關閉
onbeforeunload="return '是否現在離開此頁面';">

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.