Reusable LoadText Function

Hi,

I want to write a function for loading html text from a file, but I’m having trouble figuring out how to use LoadVars correctly in that manner. I’m not loading speific variables, but a chunk of html.

I’d like to be able to do something like this:

** myText = loadHtml(“blah.html”);
myTextField.htmltext = myText;**

This is my current code, but I don’t know how to pass the data back out of the function.

** loadHtml = new LoadVars();
loadHtml.onLoad = function() {
trace(this) // traces the text file.
};
loadHtml.load(“data.html”);**

I found this, and modified it a little.
http://proto.layer51.com/d.aspx?f=370

loadText = function(url, textfield){   
    
    var textml = new XML();
    textml.textfield = textfield;

    textml.onLoad = function(src){
        textfield.text = this;
    };
    textml.load(url);
}

This is what I use…
NB: my dynamic text box is at _root with an instance name of txt

txt.html looks like this:

&blah=<font face=“arial”><b>A TITLE</b></font><br>
<font face=“courier”>some blah blah</font><br>
<a href=“http://www.google.com” target=“blank”><u>go to google</u></a>

actionscript look like this:


// -------------------------------------------------------
with (txt) {
	//scale vertically, autosize, render as html
	wordWrap=true, autoSize=true, html=true;
}
function loadtext() {
	myVars = new LoadVars();
	myVars.n = this;
	myVars.onLoad = function(success) {
		if (success) {
			txt.htmlText = this.blah;
                        // call other init functions from here (eg scrolling) after text is loaded
		} else {
			trace("Error!");
		}
	};
	myVars.load("text.html");
}
loadtext();

^ Also… as far as I know it isnt possible to import an actual html file into a text box. So you can call it text.txt If you want.

I also use this same code to call a .php file which queries a database then sends back the same thing. No problems with it so far.

hope this helps :cowboy:

Cheers bootiteq, some good ideas there about error handling. With the XML object you can actually load a plain text or HTML file, without needing to define variables in it. It’s not a well documented feature though.

Since the last post, I’ve re-written my code into a textfield prototype:

TextField.prototype.loadText = function(fileName){ 

    trace("Loading Text: " + fileName);    
    
    var textml = new XML();
    textml.textfield = this;
    textml.fileName = fileName;

    textml.onLoad = function(success){
        // Set Message
        if (success) {
            msg = this;
        } else {
            msg = "Loading Text Failed: " + this.fileName;
        }

        // Set Text
        if (this.textfield.html) {
            this.textfield.htmlText = msg;
        } else {
            this.textfield.text = msg;
        }        
    };
    textml.load(fileName);
}

Usage:


myTextfield.loadText("data.txt");

// (or, if you want to import HTML)
myTextfield.html = true;
myTextfield.loadText("data.html");

nice work kevinc! :thumb:

What do you mean by html file?:rocker:
all i got was muck when i loaded fully tagged html…

May be the encoding i used here on this mac. (unix - mac roman)

any thoughts?

Well, it just loads whatever basic HTML Flash textfields support. e.g. something like this:

Quisque convallis viverra urna. Mauris feugiat. Mauris tempor scelerisque massa. <br> At hendrerit magna sapien vel enim. <b>Duis fringilla</b> erat id pede. Phasellus vehicula imperdiet ligula.

What are you getting back exactly? I’ve actually changed it slightly now, to use loadVars instead. I’ve attached my code with sample files. Might be a different between mac/windows files, but give it a whirl.

Hmmm yeah didnt think it was possible. When I loaded a html file. The result was the same as what you get when you open a movie in notepad.
the text came thru but it was surrounded by odd characters.

I often run into problems on a mac when saving certain files like asp or xml documents to both our office server which is windows 2000 and when uploading to our IIS windows web server.

Depending on what kind of file it is, changing the line endings and file encodings makes a huge difference.

Ive been pestering for a PC :moustache

Well, at least it works in principle for you. :slight_smile: You can probably take it and combine it with your code to solve the characters issue.

You could try setting System.useCodepage = true; - I know that’s solved problems for me in the past.

All the best!