Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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)
        }
      }
    }
    Last edited by Kor; 09-06-2011 at 03:26 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    SJamG (09-06-2011)

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Here's some documentation and examples regarding AJAX:
    http://www.jibbering.com/2002/4/httprequest.html
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    SJamG (09-06-2011)

  • #9
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you all very much - It is working a treat now.

    Thanks again.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •