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

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 11-04-2011, 12:56 AM   PM User | #1
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
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();
}
jmrker is offline   Reply With Quote
Old 11-04-2011, 07:43 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
make an AJAX HEAD* call and check the status code (i.e. 200/404).



* - like GET, but only headers are sent back.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 11-04-2011, 02:51 PM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
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.
jmrker is offline   Reply With Quote
Old 11-04-2011, 03:45 PM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
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'); }
}
jmrker is offline   Reply With Quote
Old 11-04-2011, 04:14 PM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
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'); }

}
jmrker is offline   Reply With Quote
Reply

Bookmarks

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 01:47 AM.


Advertisement
Log in to turn off these ads.