Use the syntax of the ActionScript to write the html5--nineth, imitate Urlloader read the file

Source: Internet
Author: User
Tags addchild readfile

Nineth, imitation urlloader read file

Let's take a look at the final code

function ReadFile () {    new  lurlloader ();    Urlloader.addeventlistener (Levent.complete,readfileok);    Urlloader.load (".. /file/test.txt "," text ");} function Readfileok () {    = urlloader.data;}

Basically, the simulation of ActionScript has been realized.

Effects and code see here, see the effect of the download support HTML5 browser

Http://fsanguo.comoj.com/html5/jstoas09/index.html

Let's talk about the implementation process.
In fact, JavaScript in the ActiveXObject can realize the local file read and write, but your browser security level must be set to a minimum, but we do the game and the page is to be put on the Internet, we have no way to ask all users to do so.

Here, I use PHP to implement this process, PHP can freely read the file on the server, it does not depend on the user's browser settings

Reading the file in PHP is very simple, a fopen function can be done, the following is the code of file.php

if (!file_exists ($_post["file"]) {        "";        Exit;    }     = fopen ($_post["file"], "R");     = "";      while (! feof ($file)) {        = fgets ($file);         = $line;    }    Fclose ($file);    echo $filemsg;

Put this PHP where you like, then set the path in the Legend.js legend_file_php point to where you put it

About JavaScript call PHP, of course, can write their own, because it is not complicated, but I am a very lazy person, so I directly use jquery to invoke, what is jquery? I don't think I have to explain it.

About the Lurlloader structure, and Lloader basically, only the load method is different, the following is the complete code of the Lurlloader class, which called the previously prepared PHP to get the text to read

functionLurlloader () {varSelf = This; Self.objectindex= ++Lglobal.objectindex; Self.type= "Lurlloader"; Self.loadtype= ""; Self.content=NULL; Self.oncomplete=NULL; Self.event= {};} Lurlloader.prototype={addeventlistener:function(type,listener) {varSelf = This; if(Type = =levent.complete) {Self.oncomplete=Listener; }}, Load:function(path,loadtype) {varSelf = This; Self.loadtype=Loadtype; if(Self.loadtype = = "Text"{$.post (legend_file_php, {FLG):"Read", File:PATH},function(data) {if(self.oncomplete) {Self.event.currentTarget=data; Self.data=data;                Self.oncomplete (self.event);        }            }); }    }}

For the above example, I added a button, a Ltextfield, the code to see below

Init (600,500, "Mylegend",, main);varLoadinglayer;varBacklayer;varUrlloadervarMytxt;functionMain () {legendloadover (); varREADBTN = AddButton ("read", 20); Readbtn.x= 10; READBTN.Y= 20;    AddChild (READBTN);        Readbtn.addeventlistener (Lmouseevent.mouse_down, readFile); Mytxt=NewLtextfield (); Mytxt.x= 10; Mytxt.y= 50; Mytxt.text= ""; Mytxt.width= 300; Mytxt.height= 200;    Mytxt.settype (Ltextfieldtype.input); AddChild (mytxt);}functionReadfileok () {Mytxt.text=Urlloader.data;}functionReadFile () {Urlloader=NewLurlloader ();    Urlloader.addeventlistener (Levent.complete,readfileok); Urlloader.load (".. /file/test.txt "," text ");}functionAddButton (lbl,x) {varup =NewLsprite (); Up.graphics.drawRect (1, "Black", [0, 0, 80, 20],true, "#999999"); varRX2 =NewLtextfield (); Txt.x=x; Txt.text=LBL;    Up.addchild (TXT); varover =NewLsprite (); Over.graphics.drawRect (1, "Black", [0, 0, 80, 20],true, "#cccccc"); varTXT1 =NewLtextfield (); txt1.x=x; Txt1.text=LBL;    Over.addchild (TXT1); varBTN =NewLbutton (Up,over); returnbtn;}

Over, the mock ActionScript reads the text file complete

Use the syntax of the ActionScript to write the html5--nineth, imitate Urlloader read the file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.