...

View Full Version : Check if Page exists



SJamG
09-06-2011, 02:54 PM
Hello,

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";

checkUrl(url);

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");
}</script>

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.

SJG

Kor
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:

http://www.troywolf.com/articles/

SJamG
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?

Thanks

Kor
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.

SJamG
09-06-2011, 03:16 PM
Interesting....

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

http://my-server.mycompany.com/DocLibrary/Test.pdf

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.

Kor
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()
xmlhttp.onreadystatechange=xmlhttpChange
xmlhttp.open("HEAD",url,true)
xmlhttp.send(null)
}
// old IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)
{
xmlhttp.onreadystatechange=xmlhttpChange
xmlhttp.open("HEAD",url,true)
xmlhttp.send();
}
}
}

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

Kor
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.

Kor
09-06-2011, 03:46 PM
Here's some documentation and examples regarding AJAX:
http://www.jibbering.com/2002/4/httprequest.html

SJamG
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