js判断url是否有效的两种方法

JavaScript014

js判断url是否有效的两种方法,第1张

js判断url是否有效的两种方法如下:

方法一:(仅适用于ie)

function CheckStatus(url)

{

XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")

XMLHTTP.open("HEAD",url,false)

XMLHTTP.send()

return XMLHTTP.status==200

}

function NetPing()

{

return CheckStatus("http://www.baidu.com")

}

方法二:(利用jquery,适用所有浏览器)

function NetPing() {

$.ajax({

type: "GET",

cache: false,

url: "http://www.baidu.com",

data: "",

success: function() {

Done(1)

},

error: function() {

Done(0)

}

})

}

function isvalidurl( chars ) {// chars = chars.replace(',',)//var re=/^([hh][tt]{2}[pp]://|[hh][tt]{2}[pp][ss]://)((((w+(-*w*)+).)+((com)|(net)|(edu)|(gov)|(org)|(biz)|(aero)|(coop)|(info)|(name)|(pro)|(museum))(.([a-z]{2}))?)|((w+(-*w*)+).(cn)))$/var re=/^([hh][tt]{2}[pp]://|[hh][tt]{2}[pp][ss]://)(s+.s+)$///var re=/^([hh][tt]{2}[pp]://|[hh][tt]{2}[pp][ss]://)(((((w+(-*w*)+).)+((com)|(net)|(edu)|(gov)|(org)|(biz)|(aero)|(coop)|(info)|(name)|(pro)|(me)|(cc)|(museum)|(cn)|(tv)|(hk))(.([a-z]{2}))?)|((w+(-*w*)+).(cn)))((/|?)s*)*)$/if (!isnull(chars)) {chars = jstrim(chars)if (chars.match(re) == null)return falseelse