开发者

Is there any way to check is given link really exist?

For example there are lalalalaal.com that do NOT exist. Is there any way using JavaScript (possibly开发者_StackOverflow社区 with jQuery) to check is given link really exist?


There has to be a server involved because of the Same Origin Policy — but it doesn't necessarily follow that it has to be your server. :-)

You can use a third-party service such as Yahoo to do the proxying for you as discussed here: "Using YQL as a proxy for cross-domain Ajax". That shows how to use jQuery to query YQL's JSON-P and JSON-P-X interfaces for another domain's content.

It's not complicated, from the article:

$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
          "q=select%20*%20from%20html%20where%20url%3D%22"+
          encodeURIComponent(url)+
          "%22&format=xml'&callback=?",
  function(data){
    if(data.results[0]){
      container.html(data.results[0]);
    } else {
      var errormsg = '<p>Error: could not load the page.</p>';
      container.html(errormsg);
    }
  }
);


There isn't, the response from another domain will always be null (the same origin policy applies here). You'd have to contact your own domain and have it check server-side if the site actually exists...but JavaScript alone can't do this.


No, there isn't.

In order to find out if a URL exists, you have to make a request to it and see if you get a response. The same origin policy prevents JavaScript, running in a browser under normal security conditions, from reading responses from different hosts.


I don't think you can, for security reasons (same domain policy).


Use AnyOrigin or the FOSS clone I made of it, WhateverOrigin.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜