


Use ActionScript-like syntax to write html5 - Part 9, imitating URLLoader to read files
Part 9, imitating URLLoader to read files
Let’s take a look at the final code first
function readFile(){ urlloader = new LURLLoader(); urlloader.addEventListener(LEvent.COMPLETE,readFileOk); urlloader.load("../file/test.txt","text"); } function readFileOk(){ mytxt.text = urlloader.data; }
Basically, the imitation of Actionscript has been realized.
Look here for the effect and code. If you cannot see the effect, please download a browser that supports HTML5
http://fsanguo.comoj.com/html5/jstoas09/index.html
Let’s talk about the implementation process
In fact, ActiveXObject in JavaScript can read and write local files, but the security level of your browser must be set to the lowest, but what we do Games and web pages must be put online, and there is no way we can require all users to do so.
Here, I use PHP to implement this process. PHP can freely read files on the server. It does not depend on the user's browser settings.
Read files with PHP It's very simple. It can be done with a fopen function. The following is the code of file.php
if(!file_exists($_POST["file"])){ echo ""; exit; } $file = fopen($_POST["file"],"r"); $filemsg = ""; while (!feof($file)) { $line = fgets($file); $filemsg = $line; } fclose($file); echo $filemsg;
Put this php in the location you like, and then set the path LEGEND_FILE_PHP in legend.js to point to the location you put
Regarding javascript calling php, of course you can write it yourself, because it is not complicated, but I am a very lazy person, so I directly use jQuery to call it. What is jquery? I guess I don’t need to explain it
Regarding the structure of LURLLoader, it is basically the same as LLoader, only the load method is different. The following is the complete code of the LURLLoader class, which calls the previously prepared php to obtain the text to be read.
function LURLLoader(){ var self = this; self.objectindex = ++LGlobal.objectIndex; self.type="LURLLoader"; self.loadtype = ""; self.content = null; self.oncomplete = null; self.event = {}; } LURLLoader.prototype = { addEventListener:function(type,listener){ var self = this; if(type == LEvent.COMPLETE){ self.oncomplete = listener; } }, load:function (path,loadtype){ var self = 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); } }); } } }
Regarding the above example, I added a button and a LTextField, the code is as follows
init(40,"mylegend",600,500,main); var loadingLayer; var backLayer; var urlloader var mytxt; function main(){ legendLoadOver(); var readBtn = addButton("读取",20); readBtn.x = 10; readBtn.y = 20; addChild(readBtn); readBtn.addEventListener(LMouseEvent.MOUSE_DOWN, readFile); mytxt = new LTextField(); mytxt.x = 10; mytxt.y = 50; mytxt.text = ""; mytxt.width = 300; mytxt.height = 200; mytxt.setType(LTextFieldType.INPUT); addChild(mytxt); } function readFileOk(){ mytxt.text = urlloader.data; } function readFile(){ urlloader = new LURLLoader(); urlloader.addEventListener(LEvent.COMPLETE,readFileOk); urlloader.load("../file/test.txt","text"); } function addButton(lbl,x){ var up = new LSprite(); up.graphics.drawRect(1,"black",[0, 0, 80, 20],true,"#999999"); var txt = new LTextField(); txt.x = x; txt.text = lbl; up.addChild(txt); var over = new LSprite(); over.graphics.drawRect(1,"black",[0, 0, 80, 20],true,"#cccccc"); var txt1 = new LTextField(); txt1.x = x; txt1.text = lbl; over.addChild(txt1); var btn = new LButton(up,over); return btn; }
The above is to use the syntax of imitating ActionScript to write html5 - Part 9, imitating URLLoader reading Get the content of the file. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
