關閉瀏覽器時提示onbeforeunload事件_javascript技巧

來源:互聯網
上載者:User

onbeforeunload事件 它是這樣用的:

複製代碼 代碼如下:

<script language="javascript"> 
    g_blnCheckUnload = true; 
    function RunOnBeforeUnload() {     
        if (g_blnCheckUnload) {
        window.event.returnValue = 'You will lose any unsaved content';       
        }   
    }
</script>
<body  onbeforeunload="RunOnBeforeUnload()">
</body>

這樣每次關閉瀏覽器視窗或涮新頁面時,都要觸發onbeforeunload這個事件。

摘抄如下:onbeforeunload事件:

註:三大主流瀏覽器中firefox和IE都支援onbeforeunload事件,opera尚未支援。

用法:

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+

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.