如何测试jQuery中的URL [英] How to test a URL in jQuery

查看:83
本文介绍了如何测试jQuery中的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想在一个的fancybox (或任何其他叠加式弹出打开URL向上)。我不知道事先URL是否是好,所以我想对它进行测试。如果无效的 - 我不会插件的fancybox附加到特定的URL,这将只是一个普通的链接。 如何安装一个插件,它之前进行测试的网址试着做类似:?

I have a URL which I want to open in a FancyBox (or any other overlay-type pop-up). I don't know in advance whether URL is good, so I want to test it. If invalid - I won't attach the fancyBox plugin to that particular URL, it will just be a regular link. How can I test a URL before attaching a plugin to it? Tried doing something like:

$("a.overlay").each(function() {
    var xhr = $.get(this.href, function(data, status) {
        // never executed in case of a failed request
    });
    if (xhr.status && xhr.status === 404)) {
        // not good, do nothing
    } else {
        // can attach plugin here
    }
});

现在的问题是, XHR 将不能总是限定,因为JS不等待请求完成。同样的,我不能使用的回调函数,因为它似乎并没有被执行的情况下,请求将失败(我可以看到它在Firebug的,但是这不是非常有用)。

The problem is, xhr will not always be defined because JS doesn't wait for the request to complete. Similarly, I cannot use the callback function, because it doesn't seem to be executing in case the request fails (I can see it in Firebug, but that's not very useful).

感谢,并有一个美好的周末大家。

Thanks and have a good weekend everyone.

推荐答案

检查出的这个jQuery插件。它确实HEAD请求。然后你只需要在申请过程中,检查是否存在错误,并确保在返回的状态code = = 200。

Check out this jquery plugin. It does a HEAD request. Then you just need to check for errors during the request and make sure that the returned status code == 200.

这篇关于如何测试jQuery中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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