...

View Full Version : Check an external url existence using javascript



nijicp
09-22-2011, 04:38 PM
I need to check the whether an external url like http://www.somesite.com exists or not using javascript.

I have a ajax method for it,


function checkUrl(url) {
var xmlhttp=false;

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}

xmlhttp.onreadystatechange=function() {

if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
alert("URL Exists!");
}
else if (xmlhttp.status==404) {
alert("URL doesn't exist!");
}
else {
alert("Status is "+xmlhttp.status);
}
}
}
xmlhttp.open("HEAD", url ,true);
xmlhttp.send();
}

But is not successful for finding existence of external urls.

Any help...
Thanks in advance..

Raphael
09-23-2011, 04:24 PM
It's probably not working due to XSS security issues. You could try using a hidden iframe and loading the page in it maybe.

Philip M
09-23-2011, 04:55 PM
Same Origin Policy

JavaScript automatically prevents scripts on one server from accessing properties of documents on a different server. This restriction prevents scripts from, for example, fetching private information such as directory structures or user session history

Simply stated, the SOP states that JavaScript code running on a web page may not interact with any resource not originating from the same web site.

The SOP limits any AJAX XMLHTTPRequest browser call to URLs on the same server from which the host page was loaded. This means that it is impossible to make any kind of AJAX-style request to a different site than the one from which the page was loaded.



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum