Enjoy an ad free experience by logging in. Not a member yet?
Register .
11-04-2011, 12:56 AM
PM User |
#1
Senior Coder
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
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();
}
11-04-2011, 07:43 AM
PM User |
#2
Senior Coder
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
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
11-04-2011, 02:51 PM
PM User |
#3
Senior Coder
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
Quote:
Originally Posted by
Dormilich
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.
11-04-2011, 03:45 PM
PM User |
#4
Senior Coder
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
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'); }
}
11-04-2011, 04:14 PM
PM User |
#5
Senior Coder
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
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'); }
}
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 01:47 AM .
Advertisement
Log in to turn off these ads.