Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-06-2011, 02:54 PM   PM User | #1
SJamG
New to the CF scene

 
Join Date: Sep 2011
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SJamG is an unknown quantity at this point
Check if Page exists

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 :

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
SJamG is offline   Reply With Quote
Old 09-06-2011, 03:01 PM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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/
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 09-06-2011, 03:04 PM   PM User | #3
SJamG
New to the CF scene

 
Join Date: Sep 2011
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SJamG is an unknown quantity at this point
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
SJamG is offline   Reply With Quote
Old 09-06-2011, 03:11 PM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
ok. You need also to check the readyState property of the AJAX request object
Code:
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.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 09-06-2011, 03:16 PM   PM User | #5
SJamG
New to the CF scene

 
Join Date: Sep 2011
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SJamG is an unknown quantity at this point
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.
SJamG is offline   Reply With Quote
Old 09-06-2011, 03:20 PM   PM User | #6
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
And you need an event, also, which should be the onreadystatechange.

A rough AJAX request could be detailed as:
Code:
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
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 09-06-2011 at 03:26 PM..
Kor is offline   Reply With Quote
Users who have thanked Kor for this post:
SJamG (09-06-2011)
Old 09-06-2011, 03:24 PM   PM User | #7
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 09-06-2011, 03:46 PM   PM User | #8
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Here's some documentation and examples regarding AJAX:
http://www.jibbering.com/2002/4/httprequest.html
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Users who have thanked Kor for this post:
SJamG (09-06-2011)
Old 09-06-2011, 03:57 PM   PM User | #9
SJamG
New to the CF scene

 
Join Date: Sep 2011
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SJamG is an unknown quantity at this point
Thank you all very much - It is working a treat now.

Thanks again.
SJamG is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 PM.


Advertisement
Log in to turn off these ads.