View Full Version : Check if Page exists

09-06-2011, 02:54 PM

Really need some help with this code i found online to check to see if a page exists. i am kind of new to Javascript and to have this working would really help me out!

Here is the code :

<script language="javascript">

var url = "http://www.google.com";


function checkUrl(url) {
var request = false;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest;
} else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHttp");

if (request) {
request.open('GET', url, true);
if (request.status == 200) { //FAILING ON THIS LINE ("Unspecified Error")
alert ("File does exist");
alert("File does not exist");

It seems to be failing on the .status line and it returns an Unspecified Error in the debugger.

I am using IE9 if that makes any difference. I have also tried amending the IF statement to include the readyState == 4, i dont get the error, but it doesn't work as planned.

Really appreciate any help.


09-06-2011, 03:01 PM
You can not perform an AJAX request cross-domain. You need an extra, intermediate, proxy server-side code for that. See also:


09-06-2011, 03:04 PM
Thanks for the very quick reply.

I put google.com in there just for illustration. I get the same error with my internal URL. The code is on a page which is on the same domain/site.

Does your advice still apply?


09-06-2011, 03:11 PM
ok. You need also to check the readyState property of the AJAX request object

if (request.readyState==4 && request.status == 200)

Another thing. To check only if a file exists, it is enough to check if the HEAD exists, thus you may use the HEAD request method instead of GET or POST. You gain speed.

09-06-2011, 03:16 PM

I am actually looking to open PDFs which are on a server, an example of the url would be :


I did try and alter the IF statement similar to what you suggested, but it always comes back as the file exists, but i if i load the URL in a new window, i get Page cannot be displayed.

Am i going wrong somewhere?

Thanks again for the help...appreciate it.

09-06-2011, 03:20 PM
And you need an event, also, which should be the onreadystatechange.

A rough AJAX request could be detailed as:

var xmlhttp;
function checkURL(url){
xmlhttp=null; // initialize the request object
// All the browsers except for the old IE
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest()
// old IE
else if (window.ActiveXObject)
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)

function xmlhttpChange()
// if loaded
if (xmlhttp.readyState==4)
// if head exists "OK"
if (xmlhttp.status==200)
alert('URL exists')
alert("Status is "+xmlhttp.status)

09-06-2011, 03:24 PM
But I will rather send a simple AJAX request to a server-side file and let the server-side code written there(say, in PHP) to check if a file exists and to echo back the answer.

09-06-2011, 03:46 PM
Here's some documentation and examples regarding AJAX:

09-06-2011, 03:57 PM
Thank you all very much - It is working a treat now.

Thanks again.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum