请问用javascript检测网络是否连通 怎么判断呢?

JavaScript012

请问用javascript检测网络是否连通 怎么判断呢?,第1张

正常情况下,其它浏览器能够自动检测是否具有网络链接,但是 google 浏览器在这个方面有 BUG,很多网站采用的一种做法是不断的向服务器发送请求,确保连接的稳定,如果连不上了,就说明网络断开了,所以你的问题就可以采用类似的办法,下面是 js 代码:

function isOnline(){

  var img = new Image()

  img.id = "test_is_online"

  img.onload = function(){

    document.body.removeChild(

         document.getElementById("test_is_online"))

  }

  img.onerror = function(){

    document.body.removeChild(

        document.getElementById("test_is_online"))

    alert("断网了!")

  }

  img.src = "http://www.baidu.com/img/baidu_jgylogo3.gif"

  img.style.display = "none"

  document.body.appendChild(img)

}

window.onload = function(){

    setInterval(isOnline, 10000)

}

每 10 秒钟检测一次,如果发现已经断网了,就会弹出对话框说:断网了

if ($('input[name="iIsRedirect"]').prop('checked') &&p2['sRedirectURL'] != '') {

var sRedirectURL = $('input[name="sRedirectURL"]').val(),

reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g //正则表达式判断http://https:// 为合法

objExp = new RegExp(reg)

if (objExp.test(sRedirectURL) != true) {

layer.alert('跳转地址不正确!')

return

}

}

reg = /^(http:\/\/|^https:\/\/|^\/\/)((\w|=|\?|\.|\/|&|-)+)/g//正则表达式判断http://https://

// 开头都为合法

此方法判断 获取到的一个url跟正则匹配 是否是有效地址

<script language="javascript">

var i=<%=request("link")%>

if(i==1)

{

document.getElementById("hot_1").click()

}

</script>

哦!我明白你的意思了

可以这样

<script language="javascript">

function requestGet()

{

var name,value

var str=location.href//取得整个地址栏

var num=str.indexOf("?")

str=str.substr(num+1)//取得所有参数

var arr=str.split("&")//各个参数放到数组里

for(var i=0i <arr.lengthi++){

num=arr[i].indexOf("=")

if(num>0){

name=arr[i].substring(0,num)

value=arr[i].substr(num+1)

this[name]=value

}

}

}

var Request=new requestGet()//实例化

var i=Request.link

if(i==1)

{

document.getElementById("hot_1").click()

}

</script>