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 5 of 5
  1. #1
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,073
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question file exists function available?

    I am using the code below to load a text file from the server into a <textarea> of the HTML.

    Is there a function that checks if the filename was found?
    Currently, it fills the textarea with a 'file not found' HTML page.
    What I would like to know is if the file was found before I load in the garbage information.


    Code:
    function el(tid) { return document.getElementById(tid); }
    
    var myAjax = {
      TextInformation : '',
    // From: http://codingforums.com/showthread.php?t=143412&highlight=file
      myAjaxIO : function (U, V) { //LA MOD String Version. A tiny ajax library.  by, DanDavis
        var X = !window.XMLHttpRequest 
    	  ? new ActiveXObject('Microsoft.XMLHTTP') 
    	  : new XMLHttpRequest();
        X.open(V ? 'PUT' : 'GET', U, false );
        X.setRequestHeader('Content-Type', 'text/html');
        X.send(V ? V : '');
        return X.responseText;
      },
      doLoad : function (filename) { this.TextInformation = this.myAjaxIO(filename); },
      getTextInfo : function () { // alert(this.TextInformation);  // for testing purposes
        return this.TextInformation }
    }
    
    function Load_and_Show(filename) {
      myAjax.doLoad(filename);
      el('TArea').value=myAjax.getTextInfo();
    }

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,240
    Thanks
    12
    Thanked 340 Times in 336 Posts
    make an AJAX HEAD* call and check the status code (i.e. 200/404).



    * - like GET, but only headers are sent back.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,073
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Arrow

    Quote Originally Posted by Dormilich View Post
    make an AJAX HEAD* call and check the status code (i.e. 200/404).



    * - like GET, but only headers are sent back.
    Thanks - I'll google that and look into it.
    I just did not know the words to look for in my search.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,073
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    Could you take a peek at this?

    Error console says "XMLHttp is not defined".
    I've also tried "xmlhttp", if that matters, with similar results.

    Code:
    var myAjax = {
      TextInformation : '',
    // From: http://codingforums.com/showthread.php?t=143412&highlight=file
      myAjaxIO : function (U, V) { //LA MOD String Version. A tiny ajax library.  by, DanDavis
        var X = !window.XMLHttpRequest 
    	  ? new ActiveXObject('Microsoft.XMLHTTP') 
    	  : new XMLHttpRequest();
        X.open(V ? 'PUT' : 'GET', U, false );
        X.setRequestHeader('Content-Type', 'text/html');
        X.send(V ? V : '');
        return X.responseText;
      },
    
      exists : function (filename) {
        var fileStatus = false;
        XMLHttp.open("HEAD",filename,true);
        XMLHttp.onreadystatechange=function() {
          if (XMLHttp.readyState==4) {
           if (XMLHttp.status==200) fileStatus = true   // alert("URL Exists!")
           else if (XMLHttp.status==404) fileStatus = false   // alert("URL doesn't exist!")
                else alert("Status is "+XMLHttp.status)
          }
        }
        XMLHttp.send(fileStatus)		//  xmlhttp.send(null)
      },
    
      doLoad : function (filename) { this.TextInformation = this.myAjaxIO(filename); },
      getTextInfo : function () { // alert(this.TextInformation);  // for testing purposes
        return this.TextInformation }
    }
    
    function Load_and_Show(filename) {
      if (myAjax.exists(filename)) {
        myAjax.doLoad(filename);
        el('TArea').value=myAjax.getTextInfo();
      } else { alert(filename+' does not exist'); }
    }

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,073
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Working now with modification ...

    Changed to the following and all seems OK now.
    Code:
    var myAjax = {
      TextInformation : '',
    // From: http://codingforums.com/showthread.php?t=143412&highlight=file
      myAjaxIO : function (U, V) { //LA MOD String Version. A tiny ajax library.  by, DanDavis
        var X = !window.XMLHttpRequest 
    	  ? new ActiveXObject('Microsoft.XMLHTTP') 
    	  : new XMLHttpRequest();
        X.open(V ? 'PUT' : 'GET', U, false );
        X.setRequestHeader('Content-Type', 'text/html');
        X.send(V ? V : '');
        return X.responseText;
      },
    
      exists : function(url) {
        var http = new XMLHttpRequest();
        http.open('HEAD', url, false);
        http.send();
        return http.status!=404;
      },
    
      doLoad : function (filename) { this.TextInformation = this.myAjaxIO(filename); },
      getTextInfo : function () { // alert(this.TextInformation);  // for testing purposes
        return this.TextInformation }
    }
    function Load_and_Show(filename) {
      if (myAjax.exists(filename)) {
        myAjax.doLoad(filename);
        el('TArea').value=myAjax.getTextInfo();
      } else { alert(filename+' does not exist'); }
    
    }


  •  

    Posting Permissions

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