Home  >  Article  >  Web Front-end  >  JavaScript判断一个URL链接是否有效的实现方法_javascript技巧

JavaScript判断一个URL链接是否有效的实现方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:01:131551browse

引言
有一个通讯录系统, 同时部署在几台服务器上, 但是主页上有个通讯录的链接, 链接到这个系统. 问题是, 有时候链接指向的服务器出故障, 于是希望在这个服务器出故障(服务不可用)的情况下, 能指向其他服务器的链接.
解决方案一: XMLHTTP方案
以下代码摘自[2]中meizz的回帖:

复制代码 代码如下:


csdn

缺点: 使用ActiveXObject, 所以是IE Only. 非IE内核浏览器不可用.
解决方案二: jQuery扩展
以下内容参考[1]
主页: http://plugins.jquery.com/project/linkchecker
Demo 页面: http://sidashin.ru/linkchecker/
下载的压缩包内有调用样例.
补充:
如果针对一个具体的URL,光用jQuery,不需要插件可以这样:
复制代码 代码如下:

$.ajax({
url: 'http://some.url.com',
type: 'GET',
complete: function(response) {
if(response.status == 200) {
alert('有效');
} else {
alert('无效');
}
}
});

参考文档:
[1]http://zhidao.baidu.com/question/138740329.html?push=ql

[2]http://topic.csdn.net/t/20041214/16/3644539.html
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn