Gra*_*eme 12 javascript jquery google-chrome onbeforeunload
我有一个系统,如果他们确定他们想要在设置脏标志后离开页面,我想与用户核实.
我正在使用以下代码 - 在FireFox中,我可以通过FireBug查看页面源,并且标记正确地在其中插入了onbeforeunload属性.
在Chrome和FireFox中,这种情况不会发生,我可以离开页面而不会发出任何警告.用于更新body标签的jQuery行肯定正在执行,它只是没有执行它.
if ($("body").attr('onbeforeunload') == null) {
if (window.event) {
// IE and Chrome use this
$("body").attr('onbeforeunload', 'CatchLeavePage(event)');
}
else {
// Firefox uses this
$("body").attr('onbeforeunload', 'return false;CatchLeavePage(event)');
}
}
Run Code Online (Sandbox Code Playgroud)
有什么想法从这里开始?
krc*_*cko 17
你不能通过返回false来中止页面卸载.您必须返回将在消息框中显示给用户的字符串,并且他决定是否要离开或留在页面上(通过选择"确定"或"取消"按钮),因此您需要编写代码,如这个:
window.onbeforeunload = function() {
return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse...
};
Run Code Online (Sandbox Code Playgroud)
小智 8
试试这个
<script type=\"text/javascript\">
var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = 'You sure you want to leave?'
function goodbye(e)
{
if(dont_confirm_leave!==1)
{
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation)
{
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}
window.onbeforeunload=goodbye;
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41721 次 |
| 最近记录: |