XML operations in JavaScript implement code _javascript techniques

Source: Internet
Author: User
Tags eval prev
JavaScript End:
Copy Code code as follows:

Initialize page
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>";
}

page = 1;
Changeimage (page);

var PageManager = document.getElementById ("Div_page");
pagemanager.innerhtml = "<a href= ' javascript:changepage (0) ' > Prev </a>"
+ "<a href= ' javascript:changepage (1) ' > next page </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>";
}

Resolves. dibi files.
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;
}

Determines the browser type. Support IE, FireFox.
function Browservalidator () {
var result;
if (!window. Domparser && windows. 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; Current page
var PageCount; Total pages

Click to change the page picture
function Changeimage (Page1) {
page = Page1;
var divimg = document.getElementById ("div_img");
divimg.innerhtml = "}

Prev Page and Next page
function Changepage (flag) {
if (flag = = 0 && page > 1) {
page--;
}
if (flag = = 1 && page < PageCount) {
page++;
}
Changeimage (page);
}


Convert JSON data to an array
function Jsontoarray (JSON) {
Return Json.replace (New RegExp ("},", "G"), "}|"). Split ("|");
}

HTML End:
Copy Code code as follows:

<title></title>
<script language= "javascript" type= "Text/javascript" charset= ' GBK ' src= ' xmlhelper.js ' ></script>
<body onload= "init ()" >
<div id= "Div_toc" ></div>
<div id= "Div_img" ></div>
<div id= "Div_page" ></div>
<div id= "Div_meta" ></div>
</body>

XML end: slightly.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.