当前位置:网站首页>Website judges network connection disconnection, JS judges network connection disconnection, best practice
Website judges network connection disconnection, JS judges network connection disconnection, best practice
2022-06-25 21:23:00 【Deng_ Xian_ Sheng】
reference :https://blog.csdn.net/ty497122758/article/details/26613775
Ideas :js There is no direct way to judge network disconnection , The general method does not work well ( As long as it is on the LAN, it will be judged as connected , Can not strictly judge whether to connect to the Internet )
Special methods , Use js Get a picture ( Baidu logo) But it won't show , Judge if you are successful ; We need to pay attention to , Most browsers have a cache ( Resources with the same file name will not be loaded repeatedly ), So take measures ; Add parameters after the picture file ( Generated by time ), Let the browser think that it is not the same picture .
Don't talk much , Code up
window.onload = function () {
// When the web page is loaded, it will run
var nextValue = true; // Used to determine whether the next cycle
setInterval(function () {
// Timing cycle ,2 Seconds at a time
if (nextValue == true) {
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 = 'https://www.baidu.com/img/flexible/logo/pc/result.png';
dom.src += '?t=' + (+new Date()); // Add parameters after the picture file ( Generated by time )
dom.id = 'map_img';
EventUtil.addHandler(dom, 'error', function () {
swal({
// amount to alert, But more beautiful
title: " The network connection is down !",
text: " Please check the network connection !",
type: "error",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: " I know it. !",
cancelButtonText: " No reminder for now !",
closeOnConfirm: true,
closeOnCancel: true
},
function (isConfirm) {
if (isConfirm) {
} else {
nextValue = false;
}
});
});
document.body.appendChild(dom);
}
}, 2000);
};
swal Provide more beautiful pop-up boxes , Import required css and js:
https://uosblog.top/ZBXYDYFX/js/sweetalert-dev.js
https://uosblog.top/ZBXYDYFX/css/sweetalert.css
It is recommended to introduce it to your website
It also needs to be introduced https://code.jquery.com/jquery-3.6.0.min.js
swal More ways to use http://mishengqiang.com/sweetalert/
effect :
边栏推荐
- 股市小白通过网上进行股票开户安全吗?
- OLED driver learning based on ssd1306 (I): SSD Chinese command table (handling)
- A simple file searcher
- 01 network basics
- Canoe learning notes (2)
- 同花顺app是正规的吗?到底安不安全
- [opencv] opencv from introduction to mastery -- detailed explanation of input and output XML and yaml files
- The beginning of manjaro's journey
- Lesson 4 beautifulsoup
- Differences between modems and routers (powercert animated videos)
猜你喜欢
Xshell mouse configuration
What is a server? (Powercert animated videos)
Idea implements hot deployment
Cvpr2019 | the latest cvpr2019 papers are the first to read!
Yolov4 reading notes (with mind map)! YOLOv4: Optimal Speed and Accuracy of Object Detection
How testers write functional test cases
[nail scenario capability package] hospital visitor verification
Finger collar pin exclusive Medal
lombok
1.0-mq getting started and using
随机推荐
Jmeter- (II) basic interface and common components for interface testing
[buucry] sensor (Manchester code)
Using two stacks to realize the function of one queue?
Lesson 4 beautifulsoup
lombok
Docker Alpine image installation PHP extension redis
C language dynamic memory allocation
Analysis and cleaning of kdevtmpfsi virus content
Send a more awesome website, which can convert curl commands into code in any language
Weed FS stress test
Cvpr2021 collection of papers | cvpr2021 latest papers | cvpr2021 review results | cvpr2021 admission results
Working principle and experimental analysis of DHCP
OBD Position Overview
[nail scenario capability package] hospital visitor verification
Vbpr (visual Bayesian personalized ranking) paper summary
109 practical shell scripts
Legal mix of settlements (utf8mb4_0900_ai_ci, implicit) and (utf8mb4_general_ci, implicit) error resolution
VFS appears when mounting a file system from an SD card: cannot open root device "mmcblk1p2“
The SH runtime directly reported an error syntax error near unexpected token ` $'. \r‘
[machine learning] machine learning from zero to mastery -- teach you to understand the principle of decision tree