Jump to content
theodore

Read textfile

Recommended Posts

If you want to go Ajax-style, I'd use this in the head:

function ajaxManager() {	var args = ajaxManager.arguments;	switch (args[0]) {		case "load_page":		if (document.getElementById) {			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();		}		if (x) {			x.onreadystatechange = function() {				if (x.readyState == 4 && x.status == 200) {					el = document.getElementById(args[2]);					el.innerHTML = x.responseText;				}			}			x.open("GET", args[1], true);			x.send(null);		}		break;		case "start_up":		ajaxManager('load_page', '[b]textfile.txt[/b]', 'content');		break;	}}

Body:

<body onload="ajaxManager('start_up')"><div id="content"></div>

Be sure to define a div named content in your CSS, if you use another name change it in your script and page.I'm pretty sure there are alternative ways. Keep 'em comming!

Share this post


Link to post
Share on other sites

I've been working on a project where I basically needed the same thing as you. Though if you ask me, AJAX is fine and dandy and all, but do you really need it if your text file is going to be static? It'd be a bigger hassle than needed, just opinion mind you. Instead, if your text file ain't nothing big, like in excess of 500kb is maybe too much, then you should probably load it up as an XML file and use XML DOM features to translate that data into HTML. I'm sure you could use more advanced XML languages, but again, seems like excess if all you need is just to put text somewhere on a page.http://www.w3schools.com/dom/default.aspOf course, you'll have to get basic XML down first, not a major hurdle though. Caveat! This, as far as I know, does not work for Safari, at least there is no current method I'm aware of that lets you load and XML document into the browser.

Share this post


Link to post
Share on other sites
This, as far as I know, does not work for Safari, at least there is no current method I'm aware of that lets you load and XML document into the browser.
Can't you use AJAX? :)

Share this post


Link to post
Share on other sites

Of course Safari can use AJAX, it has no problems using the HttpRequest Object from what I know. The problem is this.http://www.w3schools.com/dom/loadxmldoc.aspIt won't load XML files, which is the way I suggested Theodore use because it is fairly simplistic and sometimes faster. The problem's been spotted by others, I googled a few cases and people just can't get it to work with Safari. Of course I'm referring to the Windows Beta, but I presume the code for the browsers in either OS shouldn't be that drastic in comparison to where a feature like this is totally absent. So I don't reckon that Safari supports loading XML files, which is different than saying Safari does not support AJAX or even XML DOM features.

Share this post


Link to post
Share on other sites

But, using AJAX, you can get the responseXML which, at least in the other browsers, returns an XML document object that you can traverse using the XML DOM.

Share this post


Link to post
Share on other sites

I know that, but what I meant was that without AJAX, there is no method that can load for you an XML document in Safari. In my first post, I am talking exclusively about methods that do not rely on AJAX, which is the method I was describing. Also, I was only pertaining to the w3 Schools link that I posted. Well if you need more specificity as to exactly what I'm meaning and talking about, Safari doesn't support a non-AJAX solution to loading/parsing. It does not support loading/parsing like this. http://www.w3schools.com/dom/dom_parser.aspI've known it can use AJAX and properly get me the XML document, but for reasons such as preference and performance, I tend not to use AJAX when it comes to small XML files, plus there's fewer lines of code to deal with. Digressing, if you know a way to access an XML document sans AJAX in Safari, I'd be delighted if told, because I've been trying locate such a method for a while.

Share this post


Link to post
Share on other sites
However, I don't know how to use... :)
Check out the AJAX tutorial, tt might help get you started: http://www.w3schools.com/ajax/default.asp (Specifically look at the ResponseXML section).
I've known it can use AJAX and properly get me the XML document, but for reasons such as preference and performance, I tend not to use AJAX when it comes to small XML files, plus there's fewer lines of code to deal with.
Well, now I'm just curious. :)Apart from the fewer lines of code, what can you tell me about the performance differences between the implementations?IE (ActiveX):Microsoft.XMLDOM vs. Msxml2.XMLHTTPFirefox, Opera:document.implementation.createDocument("","",null) vs. XMLHttpRequest()It would appear to me that each style - the XML Parser vs the XMLHttpRequest - needs to create an HTTP connection to a server to fetch the data that is inside the file.XML Parser:
xmlDoc.async = "false";xmlDoc.load("note.xml");

XMLHttpRequest:

xmlDoc = xmlHttp.open("GET", "note.xml", false).responseXML;

Does one method do that more efficiently than the other?

Share this post


Link to post
Share on other sites
It would appear to me that each style - the XML Parser vs the XMLHttpRequest - needs to create an HTTP connection to a server to fetch the data that is inside the file.XML Parser:
xmlDoc.async = "false";xmlDoc.load("note.xml");

XMLHttpRequest:

xmlDoc = xmlHttp.open("GET", "note.xml", false).responseXML;

Does one method do that more efficiently than the other?

Well of course they both need to access the XML file, but parsing does not always require going through HTTP, i.e. if the XML file is stored locally, it doesn't go to any server. Of course, the same is said of AJAX, so it's a rather moot point.Secondly, I choose to parse XML with a browser due to simplicity over XMLHttpRequest() because, while we may be debating over milliseconds, or perhaps even less, why make a call to a server to establish a connection, then tell it to load something up, when right from the get go you can just tell the server to load something into the browser? Using XMLHttpRequest() basically gets ready to establish the connection to be made along with other prepping for the transfer first in one line of code, then to get the XML document, you'd have to use another line of code that involves responseXML. But by parsing, your local memory effectively gets the XML document into from the server/site and loads it into the variable in one action, processing nothing further from the server/site. It's a basis of making two actions to call the server vs making one action to call to the server. Why go through another two processes rather than go by one? You already know you're going to load up a document, using the XMLHttpRequest object is just excess when you can just go straight to it. Yes the difference is very small, near enough to be nuance, but I pay attention to what I write as code. Using two steps to call a server for static data inside an XML document is superfluous when one can be done. It may be a marginal efficiency, but it still bugs me, hence, I get my XML data through parsing whenever I can.

Share this post


Link to post
Share on other sites
Thank you for all of your help! I will try it later.:)
Good luck with your project.@Shonumi - Thanks for the info. Maybe once browsers have more consistent support over the XML document loader (e.g. document.implementation.createDocument), I'll take another look at it. In the meantime, I'll take that one extra step that you describe, and possibly incur a little more overhead, and continue to use AJAX for all my Mac/Safari friends out there.

Share this post


Link to post
Share on other sites
@Shonumi - Thanks for the info. Maybe once browsers have more consistent support over the XML document loader (e.g. document.implementation.createDocument), I'll take another look at it. In the meantime, I'll take that one extra step that you describe, and possibly incur a little more overhead, and continue to use AJAX for all my Mac/Safari friends out there.
Yeah, looks like I'll have to use AJAX if I'm going to make my current project work with Safari, can't win them all I guess. Mind you, I've got no problem with AJAX itself, it's wonderful for dynamic files and such, just a thing of preferences. Oh well...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...