有没有办法检查给出链接真的存在?

daG*_*vis 2 javascript jquery hyperlink

例如,有lalalalaal.com不存在.有没有办法使用JavaScript(可能与jQuery)检查给定链接真的存在?

T.J*_*der 8

由于同源策略必须涉及服务器 - 但它不一定必须是它必须是您的服务器.:-)

您可以使用Yahoo等第三方服务为您执行代理操作,如下所述:"使用YQL作为跨域Ajax的代理".这显示了如何使用jQuery查询YQL的JSON-P和JSON-PX接口以查找其他域的内容.

从文章来看,这并不复杂:

$.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);
    }
  }
);
Run Code Online (Sandbox Code Playgroud)