This article mainly introduces how js can detect whether the network is connected to a specific function. For more information, see
The Code is as follows:
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;
}
},
RemoveHandler: function (element, type, handler ){
If (element. removeEventListener ){
Element. removeEventListener (type, handler, false );
} Else if (element. detachEvent ){
Element. detachEvent ("on" + type, handler );
} Else {
Element ["on" + type] = null;
}
}
};
Var dom = document. createElement ('img ');
Dom. style = 'display: none ;';
Dom. src = 'HTTP: // www.baidu.com/img/bdlogo.gif ';
Dom. id = 'map _ img ';
EventUtil. addHandler (dom, 'error', function (){
Alert ('the network is not connected to your computer. You can open the map only after you connect to the network! ');
});
Document. body. appendChild (dom );