请问如何通过javascript检查网络连接的状态 或者能不能通过加载某个网页自动检查是否加载成功?谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以的,用XMLHTTP请求某个门户站点,如果返回400,则大概可以说明网络有问题!原来51里有一个检测网卡状态的,你可以去搜索一下! 谢谢啊,说实话,我javascript很菜能不能给写一小段code示例,谢谢了 <!-----试一试,看看这个怎么样---><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title> New Document </title><meta name="Generator" content="EditPlus"><meta name="Author" content=""><meta name="Keywords" content=""><meta name="Description" content=""></head><script language="javascript"> function chkconnectionStatus(url) { this.ret=false; this.objXml=new ActiveXObject("Msxml2.XMLHTTP.5.0"); this.objXml.open("post",url,false); this.objXml.onreadystatechange=function (){ //alert(this.objXml.readyState); if(this.objXml.readyState==4) { //alert(this.objXml.status); if(this.objXml.status==200) { this.ret=true; }else { this.ret=false; return; } } } this.objXml.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded"); try{ this.objXml.send(''); }catch(e) { this.ret=false; } return this.ret; } function chkit() { if(txturl.value=='') { alert('请输入一个测试的网站地址'); txturl.focus(); return ; } if(chkconnectionStatus(txturl.value)) { txtstatus.innerHTML='<font color="blue">可以连接</font>'; }else { txtstatus.innerHTML='<font color="red">无法连接!请确定您输入的地址是否存在或网络是否连通!</font>'; } }</script><body><input type="text" id="txturl" value="http://www.163.com" /><button onclick="javascript:chkit();">test</button><span id="txtstatus"></span></body></html> 先谢谢楼上的这个实现用了activeX,因为我要在firefox上用还有其他方法吗?谢谢 XMLHTTP在FF里也可以用的,如下:// Initializevar Try = { these: function(){ var returnValue; for(var i=0; i<arguments.length; i++){ var lambda = arguments[i]; try{ returnValue = lambda(); break; }catch(e){} } return returnValue; }}function ajaxInitRV(){ return Try.these( function() {return new ActiveXObject('MSXML2.XMLHttp.6.0')}, function() {return new ActiveXObject('MSXML2.XMLHttp.3.0')}, function() {return new XMLHttpRequest()}, function() {return new ActiveXObject('MSXML2.XMLHttp.5.0')}, function() {return new ActiveXObject('MSXML2.XMLHttp.4.0')}, function() {return new ActiveXObject('Msxml2.XMLHTTP')}, function() {return new ActiveXObject('MSXML.XMLHttp')}, function() {return new ActiveXObject('Microsoft.XMLHTTP')} ) || null;} muxrwc说的对,用XMLHTTP只能在服务端执行了,用ASP文件试试。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>检测网络</title><script type="text/javascript">var ising = function () { var wc = window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : new window.XMLHttpRequest, IE = /MSIE/.test(window.navigator.userAgent), returnValue; wc.open("get", "wc.asp", false); if (IE) { wc.onreadystatechange = function () { if (wc.readyState == 4 && wc.status == 200) { returnValue = wc.responseText; } }; wc.send(null); } else { wc.send(null); returnValue = responseText; } return eval(returnValue.toLowerCase());};alert(ising() ? "畅通无阻" : "一塌糊涂");</script></head><body></body></html> wc.asp<%Sub show Dim ajax Set ajax = Server.CreateObject("Microsoft.XMLHTTP") ajax.Open "Get", "http://www.baidu.com/", False ajax.Send Response.Write ajax.status = 200 Set ajax = NothingEnd Sub : show%> 没有搞懂,我主要是要用在firefox上啊 button 按钮能不能实现的功能? 求弹出新网页的方法,类似于广告的那种。 求助 javascript 弹出层问题 这段js代码在firefox里可以正常运行,在ie里却报缺少对象,还请大虾们指点迷津啊。 求助,JavaScript定时器的问题,在线等;帮我看看是哪里错了? HightCharts绑定后台传过来的json数据 jquery,load 中文乱码怎么办呢? 有关正则表达式非贪婪模式的问题。 本人愿意拿100分求一个代码!高手开来吃分吧! jQuery toggle()函数的使用问题 html过滤的问题 比较急 js传入变量数值问题,哪位帮忙看看
原来51里有一个检测网卡状态的,你可以去搜索一下!
能不能给写一小段code示例,谢谢了
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script language="javascript">
function chkconnectionStatus(url)
{
this.ret=false; this.objXml=new ActiveXObject("Msxml2.XMLHTTP.5.0");
this.objXml.open("post",url,false);
this.objXml.onreadystatechange=function (){
//alert(this.objXml.readyState); if(this.objXml.readyState==4)
{
//alert(this.objXml.status); if(this.objXml.status==200)
{
this.ret=true; }else
{
this.ret=false;
return;
}
}
}
this.objXml.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
try{
this.objXml.send('');
}catch(e)
{
this.ret=false;
}
return this.ret;
} function chkit()
{
if(txturl.value=='')
{
alert('请输入一个测试的网站地址');
txturl.focus();
return ;
}
if(chkconnectionStatus(txturl.value))
{
txtstatus.innerHTML='<font color="blue">可以连接</font>';
}else
{
txtstatus.innerHTML='<font color="red">无法连接!请确定您输入的地址是否存在或网络是否连通!</font>';
}
}
</script>
<body>
<input type="text" id="txturl" value="http://www.163.com" />
<button onclick="javascript:chkit();">test</button><span id="txtstatus"></span>
</body>
</html>
这个实现用了activeX,因为我要在firefox上用
还有其他方法吗?
谢谢
// Initializevar Try = {
these: function(){
var returnValue;
for(var i=0; i<arguments.length; i++){
var lambda = arguments[i];
try{
returnValue = lambda();
break;
}catch(e){}
}
return returnValue;
}
}function ajaxInitRV(){
return Try.these(
function() {return new ActiveXObject('MSXML2.XMLHttp.6.0')},
function() {return new ActiveXObject('MSXML2.XMLHttp.3.0')},
function() {return new XMLHttpRequest()},
function() {return new ActiveXObject('MSXML2.XMLHttp.5.0')},
function() {return new ActiveXObject('MSXML2.XMLHttp.4.0')},
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('MSXML.XMLHttp')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')}
) || null;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>检测网络</title>
<script type="text/javascript">
var ising = function () {
var wc = window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : new window.XMLHttpRequest,
IE = /MSIE/.test(window.navigator.userAgent), returnValue;
wc.open("get", "wc.asp", false);
if (IE) {
wc.onreadystatechange = function () {
if (wc.readyState == 4 && wc.status == 200) {
returnValue = wc.responseText;
}
};
wc.send(null);
} else {
wc.send(null);
returnValue = responseText;
}
return eval(returnValue.toLowerCase());
};
alert(ising() ? "畅通无阻" : "一塌糊涂");
</script>
</head>
<body>
</body>
</html>
<%
Sub show
Dim ajax
Set ajax = Server.CreateObject("Microsoft.XMLHTTP")
ajax.Open "Get", "http://www.baidu.com/", False
ajax.Send
Response.Write ajax.status = 200
Set ajax = Nothing
End Sub : show
%>