Home Web Front-end H5 Tutorial Use ActionScript-like syntax to write html5 - Part 9, imitating URLLoader to read files

Use ActionScript-like syntax to write html5 - Part 9, imitating URLLoader to read files

Jan 17, 2017 pm 05:00 PM
html5

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;
}
Copy after login

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;
Copy after login

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);
}
});
}
}
}
Copy after login

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;
}
Copy after login

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)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

See all articles