テキストファイルを読んで表示
XMLHttpRequest()でファイルを"GET"して、その中身を表示しましょうよと。
- hello.xul
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="chrome://global/skin/" type="text/css"?> <window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <script src="hello.js"/> <button id="load-button" label="Load" default="true" onclick="loadFile('hello.txt');" /> <button id="cancel-button" label="Cancel" onclick="closetest();" /> <description id="desc">Description</description> <label id="disp"/> </window>
- hello.js
function loadFile(inFilePath) { xmlhttp = new XMLHttpRequest(); if (xmlhttp) { xmlhttp.onreadystatechange = check; xmlhttp.open('GET', inFilePath, true); xmlhttp.send(null); } else { document.getElementById("disp").value = "read error!"; } } function check() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("desc").value = "success"; document.getElementById("disp").value = xmlhttp.responseText; } else { document.getElementById("desc").value = "failure: " + xmlhttp.readyState + xmlhttp.status; document.getElementById("disp").value = xmlhttp.responseText; } } function closetest() { window.close(); }
JavaScriptって結構C++とかJavaに似てるのね。ふむふむ。