2014-06-25 128 views
0

我正在使用javascript onunload函數來顯示一個彈出窗體,當用戶試圖離開頁面時,它顯示彈出窗口,但它不能阻止頁面加載。防止在JavaScript中加載頁面?

這裏是我的代碼

$(window).bind('unload', function(e) { 
    $("#dialog").dialog({ 
     modal: true 
    }); 

    e.preventDefault(); 
    return false; 
}); 

回答

0

應該是這樣的:

$(window).bind('beforeunload', function(e){ 
    e.preventDefault(); 
    $("#dialog").dialog({ 
     modal: true 
    }); 
    return "Look at the modal"; 
}); 

沒有jQuery的:

window.onbeforeunload = function (e) { 
    var message = "Your pop up message", 
    e = e || window.event; 
    // For IE ,Firefox and Chrome 
    if (e) { 
     // your functions go here 
     console.log("test"); 
     e.returnValue = message; 

    } else { 
     // For Safari 
     //your functions go here 
     console.log('test'); 
     return message; 
    }; 
}; 
+0

但它不是與iPad或iPhone – Mehar

+0

我不兼容用jquery知道任何解決方案,但請檢查我的答案中添加了'Without Jquery'解決方案。 –