-1

I'm using Javascript and i want check if a page/url (of another domain exist)

I tried this but it doesn't work

    $.ajax({
    type: 'HEAD',
    url: 'http://google.com',
success: function() {
        alert('1');
},
error: function() {
        alert('0');
}
});

it show me this error

XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localads-4u.com' is therefore not allowed access.

Note : the tested script is online

The Process
  • 5,913
  • 3
  • 30
  • 41
hassen zouari
  • 941
  • 2
  • 11
  • 18
  • 2
    Have you googled that error? Have you looked at this? http://stackoverflow.com/q/20035101/870729 – random_user_name Apr 17 '16 at 20:41
  • Your doing this on a file:// .... set up a localhost, so you dont get the Acess Alow Control Origin.. Also why ask this.. there are a ton of questions about this. – amanuel2 Apr 17 '16 at 20:41
  • @cale_b Present Question is different where current Question asks how to determine if resource is available, not how to also access the resource. Linked page _"I am trying to do authorization using JavaScript"_ , current page _"I'm using Javascript and i want check if a page/url (of another domain exist)"_ – guest271314 Apr 17 '16 at 21:08

1 Answers1

1

I'm using Javascript and i want check if a page/url (of another domain exist)

If only requirement is to check if the external resource is available, one possible workaround for No 'Access-Control-Allow-Origin' header is present on the requested resource. response from AJAX is to instead request .html document or url at src of <img> element. Check onerror handler for GET http://example.com/ net::ERR_NAME_NOT_RESOLVED

var img = new Image;
img.onload = function(e) {
  console.log(this, this.src)
}

img.onerror = function(e) {
  console.log(e, this)
};

img.src = "http://example.com";
guest271314
  • 1
  • 15
  • 104
  • 177
  • Ohh thanks bro :) that's what I need .. Please how can I make an if condition (if exist \do something/ else \do another thing/ ) ? Thanks again – hassen zouari Apr 17 '16 at 21:10
  • @hassenzouari _"how can I make an if condition (if exist \do something/ else \do another thing/ ) ?"_ ? Check whether `onload` or `onerror` is called – guest271314 Apr 17 '16 at 21:12
  • Like this if(img.onerror){//do something} ? Thaks again – hassen zouari Apr 17 '16 at 21:17
  • No, use the `onload` or `onerror` handlers. If resource is available at `onerror` try checking for `console` for `GET http://example.com/ net::ERR_NAME_NOT_RESOLVED` error – guest271314 Apr 17 '16 at 21:18
  • Yeah I see this error when the link isn't correct .. but i don't understand you very well ,, please can you update the Comment code ? Thanks – hassen zouari Apr 17 '16 at 21:21
  • `net::ERR_NAME_NOT_RESOLVED` appears to be chrome / chromium internal error. – guest271314 Apr 17 '16 at 22:05
  • At chrome / chromium you could create an extension which parsed `.har` entries for `content.size` of last entry – guest271314 Apr 17 '16 at 22:07