JS实时监听浏览器联网状态
2017-05-13 本文已影响411人
以为是书生
兼容浏览器写法
var EventUtil = {
addHandler: function (element, type, handler) {
if(element.addEventListener) {
element.addEventListener(type, handler, false);
}else if (element.attachEvent) {
element.attachEvent("on" + type, handler);
}else {
element["on" + type] = handler;
}
}
};
EventUtil.addHandler(window, "online", function () {
document.write("连上网了!");
});
EventUtil.addHandler(window, "offline", function () {
document.write("网络不给力,请检查网络设置!");
});