如何检测链接是否有效? [英] How to detect if a link works?

查看:86
本文介绍了如何检测链接是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道链接是否会打开。

I need to know if a link will open.

推荐答案

请参阅 Maximilian Hoffmann的回答获得更强大的解决方案。

See Maximilian Hoffmann's answer for a more robust solution.

这种方法很常见 - 劫持超时重定向到不同的URL。这种方法适合你吗?

An approach like this is common - hijack the timeout to redirect to a different URL. Would this approach work for you?

<a id="applink" href="comgooglemaps://?q=Red+Lobster+Billings">Show map</a>

<script type="text/javascript">

var backup = "http://maps.google.com/?q=Red+Lobster+Billings";

function applink(fail){
    return function() {
        var clickedAt = +new Date;
        setTimeout(function(){
            if (+new Date - clickedAt < 2000){
                window.location = fail;
            }
        }, 500);    
    };
}

document.getElementById("applink").onclick = applink(backup);

</script>

这篇关于如何检测链接是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆