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.
// -------------------------------------------------------
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();
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");
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.