2015-11-20 61 views
-2

我的網站顯示彈出框,當有人試圖離開我的網站,我想向他們展示彈出消息。我試圖使用window.onbeforeunload函數,但它不起作用。留下與消息

<script language="JavaScript"> 
    window.onbeforeunload = confirmExit; 
    function confirmExit(){ 
    return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; 
    } 
</script> 

嗨喬納森我想彈出顯示我的自定義消息。 目前它顯示默認的消息 - 這個頁面要求你確認你要離開 - 您所輸入的數據可能不被保存

+2

http://stackoverflow.com/questions/19263277/open-a-custom-popup-on-browser-window-tab-close –

+0

http://stackoverflow.com/questions/16735076/popup-之前用窗被封閉 –

+0

http://stackoverflow.com/questions/8118722/how-to-show-pop-up-survey-with-jquery-when-leaving-page – Suyog

回答

-1

核心的第二行是越野車

它應該是:

window.onbeforeunload=confirmExit(); 

你行缺少括號,()

+0

*「你行缺少括號()」 *它應該** **被他們失蹤。 –

+0

爲什麼T.J. Crowder? –

+0

因爲你爲了掛鉤事件而將一個函數分配給'onbeforeunload'屬性,所以你不想**調用**這個函數並且將它的返回值賦給這個屬性。這真的是非常基本的DOM事件處理。 –

0

試試這個。

$(window).bind('beforeunload', function() {   
     return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; 
      });