确定是否链接是有效的在用户点击就可以了 [英] Determine if Link is valid upon user clicking on it
问题描述
我提供了一个小工具,用户可以在存储,他们已经创造了他们的联系。
I offer a widget where a user can store their links that they have created.
所以,换句话说他们:
- 单击添加链接
- 模态弹出,他们输入信息:链接的标题,链接网址,顺序
- 保存,要通过AJAX数据库,然后刷新小工具来显示所有他们所创建的用户链接。
我想什么做的是,当用户点击他们的链接,确定是否存在链接(可能是不会在这个领域,所以我可以看到一个可预见的问题瓦特/ AJAX),如果它再与正常打开新窗口进行并把他们自己的快乐的方式。
What I would like do is when the user clicks on their link, determine if the link exists(Could be not on this domain so I can see a foreseeable issue w/ AJAX) if it does then proceed with normal Open new Window and send them on their merry way.
不过如果不是我想产生一个低吼像消息说像。
However if not I would like to generate a Growl Like message that says something like.
我很抱歉,链接不存在,
请验证您的链接。
I'm sorry that Link Does not exist, please verify your link.
我基本上需要用于验证客户端的基础上的链接存在的方法。这甚至可能?
I basically need the method for validating the links existence on a client side basis. Is this even possible ?
推荐答案
您可以使用jQuery附加一个点击()
事件处理程序上的各个环节,这反过来尝试加载的URL隐藏的iframe,如果成功,则在新窗口中打开链接。注意,这是链接检查的一种非常昂贵的方法。也许聪明的东西可以像一个HTTP请求头完成。
You could use jquery to attach a click()
event handler on all links, that in turn try to load the URL in a hidden iframe, if that succeeds then open the link in a new window. Note that this is a very expensive way of link checking. Maybe something smarter could be done like an HTTP head request.
编辑:
找到了解决方案(对于普通的JavaScript仅虽然)位置:在Javascript HTTP HEAD请求/阿贾克斯?
Found the solution (for plain javascript only though) here: HTTP HEAD Request in Javascript/Ajax?
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
和jQuery的解决方案,从这里传来:通过Javascript / jQuery的阿贾克斯HEAD请求一>
And a jQuery solution came from here: Ajax HEAD request via Javascript/jQuery
ajaxSizeRequest = $.ajax({
type: "HEAD",
async: true,
url: url,
success: function(message){
}
作为@马特金指出,这实际上是pretty没用跨域 - 在这种情况下我已经设置了在应用程序中proxy.aspx页面,您查询,而不是。然后,该代理将必须执行在C#中的HTTP HEAD请求,并将结果(成功/失败)返回给客户机/ jQuery的
As @Matt King pointed out these are actually pretty useless cross domain - in that case I'd set up a proxy.aspx page in your application that you query instead. This proxy would then have to execute the HTTP HEAD request in C#, and return the result (success/failure) to the client/jQuery.
这篇关于确定是否链接是有效的在用户点击就可以了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!