我的html代码需要一些帮助。我需要在其中集成javascript,在加载时检查互联网是否可用。如果激活它应该执行特定任务,否则它应该显示"没有互联网"。
`<html>
<head>
</head>
<body onload="myClickHandler();
startTime()">
<script language="JavaScript">
var t;
document.onclick = myClickHandler;
function myClickHandler() {
clearTimeout(t);
t = setTimeout("location.href='index.html'", 60000);
//for refreshing the page in every 60 sec.
}
</script>
<h1> test </h1>
<script type="text/javascript">
var url = "https://www.google.co.in";
var img = new Image();
img.src = url;
img.onerror = function()
{
// If the server is down, do that.
alert ("no connection");
}
img.onload = function()
{
// If the server is up, do this.
//some task to perform.
return true;
}
</script>
</body>
</html>`
这是我的代码,但页面在性能后继续加载我们还需要手动停止加载。
答案 0 :(得分:0)
使用以下代码。感谢this帖子。
要了解有关 navigator.online 的更多信息,请查看以下链接
https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine
<!doctype html>
<html>
<head>
<script>
function CheckOnlineStatus(msg) {
var status = document.getElementById("status");
var condition = navigator.onLine ? "ONLINE" : "OFFLINE";
var state = document.getElementById("state");
state.innerHTML = condition;
}
function Pageloaded() {
CheckOnlineStatus("load");
document.body.addEventListener("offline", function () {
CheckOnlineStatus("offline")
}, false);
document.body.addEventListener("online", function () {
CheckOnlineStatus("online")
}, false);
}
</script>
<style>
...</style>
</head>
<body onload="Pageloaded()">
<div id="status">
<p id="state">
</p>
</div>
</body>
</html>
&#13;