// 首先,先定义一个按钮。
<body>
<button id="btn">查询网络连接</button></body>
<script type="text/javascript">
var online = 0;
//1.实例化对象
var img = new Image();
//2.设置SRC地址
img.src = '01.php';
//3.添加加载事件
img.onload = function(){
console.log('加载成功');
//3.1更新连接状态
online = 1;
}
//4.添加异常事件
img.onerror = function(){
console.log('加载失败');
//4.1更新状态
online = 0;
}
//5.添加单击事件
document.getElementById('btn').onclick = function(){
//6.判断网络连接状态? 根据online判断
if(online){console.log('当前网络连接正常');
}else{
console.log('您的网络连接不太好赶紧换网线吧。。。。');
}
}
</script>