I am trying this code but it gives me a DOM Exception. What I want it to get a true/false "answer" from the function using plain Javascript.
var url = '/';
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
UrlExists(url);
FIDDLE
I got this code from this SO answer, but as said I cannot get it working...
I am trying this code but it gives me a DOM Exception. What I want it to get a true/false "answer" from the function using plain Javascript.
var url = 'http://www.google./';
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
UrlExists(url);
FIDDLE
I got this code from this SO answer, but as said I cannot get it working...
Share Improve this question edited May 23, 2017 at 10:31 CommunityBot 11 silver badge asked Jul 20, 2013 at 10:35 RikardRikard 7,80513 gold badges60 silver badges99 bronze badges 10- 2 This method will not work coz of Same origin policy. You will have to use a server side ponent to check if the URL exists. One option is using YQL as in this answer: stackoverflow./a/13041787/133198 – vsr Commented Jul 20, 2013 at 10:59
- @vsr, thank you for the idea. Is there a client-side way to do this? – Rikard Commented Jul 20, 2013 at 11:05
-
1
Client-side is possible only when the host of the
url
supports CORS(html5rocks./en/tutorials/cors). Since many sites don't support CORS, client-side solution is not fool-proof. – vsr Commented Jul 20, 2013 at 11:36 - Duplicate of: stackoverflow./questions/3922989/… – Matt Whipple Commented Jul 20, 2013 at 13:30
-
5
The only way to hack around SOP is to load the page in a way that the browser considers safer by using a hack with another element. I'd suggest trying the
onerror
handler for animg
or possiblyobject
tag to see if you can retreive the status code. In the past I've abusediframe
s similarly but there are restrictions there (I've used it effectively with fire and forget calls and or when the iframe source explicitly calls back to the parent). – Matt Whipple Commented Jul 20, 2013 at 14:18
4 Answers
Reset to default 4Doesn't detect 404 errors, but can check if the page exists or not with a setTimeout()
hack.
// Based on https://stackoverflow./a/18552771
// @author Irvin Dominin <https://stackoverflow./u/975520>
function UrlExists(url)
{
var iframe = document.createElement('iframe');
var iframeError; // Store the iframe timeout
iframe.onload = function () {
console.log("Success on " + url);
clearTimeout(iframeError);
}
iframeError = setTimeout(function () {
console.log("Error on " + url)
}, 3000);
iframe.src = url;
document.getElementsByTagName("body")[0].appendChild(iframe);
}
UrlExists('http://www.google./');
UrlExists('http://www.goo000gle.');
Cross domain does not give any status code. status code is part of the content which is received from server when server responds with yes or no. In case of cross domain server never respond to request.
the second mistake in the code is you cannot capture the status code directly without any waiting time or success event. return statement in function doesn't wait until server response to ajax request, so you cannot depend on it.
You can not call services from different URL because of the 'Same Origin Policy' in JavaScript. Some workaround are presented here:
Ways to circumvent the same-origin policy
The most simple way is to use 'iframe'. By opening an iframe with your target url, you will be able to make http request to that url.
I saw your fiddle and it consists of 2 issues:
- DOM Exception
- CORS policy
- You are getting DOM Exception because you are making synchronous calls.
Make the call synchronous by setting the last property to
true
http.open('GET', url, true);
You can read the documentation here XMLHttpRequest Parameters
And also check this out: DOM Exception-failed-to-execute-send-on-xmlhttprequest-on-chrome-only
- You can get an idea about the cors issue here