- OpenLaszlo紹介
- プログラム構造関連
- スクリプト
- Javascript基礎
- 基礎知識
- ビュー<view>
- レイアウト
- 動かす
- 入力デバイス
- 文字
- 数値
- 日付
- オブジェクト指向
- 標準コンポーネント
- データの扱い
- データ操作(基礎)
- 応用編
–
UTF8で「あそん」とだけ書かれた、改行コードの違う3つのテキストファイルを読み込んでいます。読み込んだ後にLZX側で改行コードをすべてCRに変換しています。エンコードされた文字列(化けたように見えている文字列)で青字は変換前、赤字は変換後です。
<?xml version="1.0" encoding="UTF-8"?> <canvas proxied="false" bgcolor="0xeeeeee" fontsize="15"> <method name="replace" args="text,from,to"> return text.split(from).join(to); </method> <method name="loadTextFile" args="filename"> System.useCodepage = false; var CR = String.fromCharCode(13); var LF = String.fromCharCode(10); var t = new LoadVars(); t.onData = function(data){ if(data!=undefined){ txtescape.setAttribute('text',escape(data)); data = canvas.replace(data, CR+LF, CR); data = canvas.replace(data, LF, CR); txt.setAttribute('text',data); txtescape2.setAttribute('text',escape(data)); }else{ txt.setAttribute('text',"読み込み失敗"); txtescape.clearText(); txtescape2.clearText(); } }; t.sendAndLoad(filename, t, "GET"); </method> <simplelayout/> <button onclick="parent.loadTextFile('http://www.openlaszlo-ason.com/media/utf8_crlf.txt')"> 外部テキストファイル(utf8,cr+lf)読み込み </button> <button onclick="parent.loadTextFile('http://www.openlaszlo-ason.com/media/utf8_cr.txt')"> 外部テキストファイル(utf8,cr)読み込み </button> <button onclick="parent.loadTextFile('http://www.openlaszlo-ason.com/media/utf8_lf.txt')"> 外部テキストファイル(utf8,lf)読み込み </button> <text id="txt" fgcolor="red"/> <text id="txtescape" fgcolor="blue" /> <text id="txtescape2" fgcolor="red" /> </canvas>
外部テキストファイルの読み込みはActionscriptを使ってるのですが、swf8(as2)とswf9以上(as3)とで書き方が違います。
サンプルの読み込みボタンを押すと外部テキストファイルinfo.htmlの中身をそのまま表示します。HTMLタグはOpenLaszloでサポートしている分が使えます。
テキストファイルの中身を変えても、SWFを再コンパイルせずに再読み込みできます。
(OpenLaszlo4.7.1/swf10)
<?xml version="1.0" encoding="UTF-8"?> <canvas proxied="false" bgcolor="0xeeeeee"> <!-- swf9以上限定 --> <passthrough> import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import flash.system.System; import flash.events.Event; </passthrough> <method name="replace" args="text,from,to"> return text.split(from).join(to); </method> <method name="loadFile" args="filename"> System.useCodePage = false; var myLoader = new URLLoader(); myLoader.addEventListener(Event.COMPLETE, onComplete); var myRequest = new URLRequest(filename); myLoader.load(myRequest); </method> <method name="onComplete" args="e"> var CR = String.fromCharCode(13); var LF = String.fromCharCode(10); var data = e.target.data data = canvas.replace(data, CR+LF, CR); data = canvas.replace(data, LF, CR); txt.setAttribute('text', data); </method> <simplelayout/> <button onclick="canvas.loadFile('/DEMO/info.html')">読み込み</button> <text id="txt" x="10" fontsize="12" bgcolor="white"/> </canvas>