window.onunload 在 Chrome 浏览器中无法正常工作.谁能帮我? [英] window.onunload is not working properly in Chrome browser. Can any one help me?

查看:43
本文介绍了window.onunload 在 Chrome 浏览器中无法正常工作.谁能帮我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这段代码

function winUnload() {
    alert("Unload Window");
    MyMethod();
}

window.onunload = function() { winUnload(); }

此代码在 IE 和 Firefox 中运行良好.但是这段代码在 Chrome 中不起作用.alert("Unload Window");MyMethod(); 这两个语句都不起作用.

This code is working fine in IE and Firefox. But this code is not working in Chrome. Both the statements alert("Unload Window"); and MyMethod(); are not working.

推荐答案

有一些操作在 chrome 中的 unload 事件内部不起作用.警报或确认框就是这样的东西.

There are some actions which are not working in chrome, inside of the unload event. Alert or confirm boxes are such things.

但是什么是可能的(AFAIK):

But what is possible (AFAIK):

  1. 打开弹出窗口(使用 window.open) - 但如果您的站点禁用了弹出窗口阻止程序,这将起作用
  2. 返回一个简单的字符串(在 beforeunload 事件中),它会触发一个确认框,询问用户是否要离开页面.

#2 的示例:

$(window).on('beforeunload', function() {
    return 'Your own message goes here...';
});

演示:http://jsfiddle.net/PQz5k/

这篇关于window.onunload 在 Chrome 浏览器中无法正常工作.谁能帮我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆