about_ javascript & xml

9255 ワード

久しぶりに書きました.今でもHTML 5はチケット遊びにすぎないような気がします.w 3 cの標準が早く登場することを望んで、各種ブラウザの互換性の問題とサポートの程度を死なせないでください.の
 
JavaScript:
//     
function init() {
var ary = JSONToArray(XMLReader("node","content.dibi"));
var divtoc = document.getElementById("div_toc");
pageCount = ary.length;
for(k = 0; k < ary.length; k++){
obj = eval('(' + ary[k] + ')');
divtoc.innerHTML += "<a href='javascript:changeImage(" + obj.page.substring(obj.page.length-1) + ")'>"
+ obj.label + "</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}

page = 1;
changeImage(page);

var pageManager = document.getElementById("div_page");
pageManager.innerHTML = "<a href='javascript:changePage(0)'> </a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
+ "<a href='javascript:changePage(1)'> </a><br/><br/>";

var ary2 = JSONToArray(XMLReader("meta","content.dibi"));
var divmeta = document.getElementById("div_meta");
var styStr = "<table>"
for(l = 0; l < ary2.length; l++){
obj2 = eval('(' + ary2[l] + ')');
styStr += "<tr><td>" + obj2.name + "</td><td>" + obj2.content + "</td></tr>";
}
divmeta.innerHTML = styStr + "</table>";
}

// .dibi 。
function XMLReader(key,fileName) {
var parse = BrowserValidator();
parse.load(fileName);
var json = "";
try{
var dom = parse.documentElement;
var attrLength = 0;
for (i = 0; i < dom.getElementsByTagName(key).length; i++) {
attrLength = dom.getElementsByTagName(key)[i].attributes.length;
objMsg = ",{";
for(j = 0; j < attrLength; j++){
objMsg += "'" + dom.getElementsByTagName(key)[i].attributes[j].name
+ "':'" + dom.getElementsByTagName(key)[i].attributes[j].value + "',";
}
objMsg = objMsg.substring(0,objMsg.length-1);
json += objMsg + "}";
}
json = json.substring(1);
}catch(e){}
return json;
}

// 。 IE ,fireFox。
function BrowserValidator(){
var result;
if(!window.DOMParser && window.ActiveXObject) {
result = new ActiveXObject("Microsoft.XMLDOM");
result.async = false;
}
else if(document.implementation && document.implementation.createDocument) {
result = document.implementation.createDocument("", "", null);
result.async = false;
}
return result;
}

var page = 1; //
var pageCount; //

//
function changeImage(page1){
page = page1;
var divimg = document.getElementById("div_img");
divimg.innerHTML = "<img width='500' src = 'images/" + page +".jpg'/>";
}

//
function changePage(flag){
if(flag == 0 && page > 1){
page--;
}
if(flag == 1 && page < pageCount){
page++;
}
changeImage(page);
}


// JSON
function JSONToArray(json){
return json.replace(new RegExp("},", "g"), "}|").split("|");
}

 
HTML:
<html>
<head>
<title></title>
<script language="javascript" type="text/javascript" charset='gbk' src="xmlhelper.js"></script>
</head>
<body onload="init()">
<div id="div_toc"></div>
<div id="div_img"></div>
<div id="div_page"></div>
<div id="div_meta"></div>
</body>
</html>

 
XML端:略.