2014-06-25 89 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'解决方案。 –