daGrevis
daGrevis

Reputation: 21333

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 with jQuery) to check is given link really exist?

Upvotes: 2

Views: 308

Answers (5)

ripper234
ripper234

Reputation: 230008

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

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074168

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);
    }
  }
);

Upvotes: 8

alex
alex

Reputation: 490173

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

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630379

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.

Upvotes: 2

Quentin
Quentin

Reputation: 943214

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.

Upvotes: 0

Related Questions