source: main/trunk/greenstone3/web/xmlextras.js@ 26581

Last change on this file since 26581 was 10695, checked in by kjdon, 19 years ago

Dave's basic AJAX search stuff

  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1//<script>
2//////////////////
3// Helper Stuff //
4//////////////////
5
6// used to find the Automation server name
7function getDomDocumentPrefix() {
8 if (getDomDocumentPrefix.prefix)
9 return getDomDocumentPrefix.prefix;
10
11 var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
12 var o;
13 for (var i = 0; i < prefixes.length; i++) {
14 try {
15 // try to create the objects
16 o = new ActiveXObject(prefixes[i] + ".DomDocument");
17 return getDomDocumentPrefix.prefix = prefixes[i];
18 }
19 catch (ex) {};
20 }
21
22 throw new Error("Could not find an installed XML parser");
23}
24
25function getXmlHttpPrefix() {
26 if (getXmlHttpPrefix.prefix)
27 return getXmlHttpPrefix.prefix;
28
29 var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
30 var o;
31 for (var i = 0; i < prefixes.length; i++) {
32 try {
33 // try to create the objects
34 o = new ActiveXObject(prefixes[i] + ".XmlHttp");
35 return getXmlHttpPrefix.prefix = prefixes[i];
36 }
37 catch (ex) {};
38 }
39
40 throw new Error("Could not find an installed XML parser");
41}
42
43//////////////////////////
44// Start the Real stuff //
45//////////////////////////
46
47
48// XmlHttp factory
49function XmlHttp() {}
50
51XmlHttp.create = function () {
52 try {
53 if (window.XMLHttpRequest) {
54 var req = new XMLHttpRequest();
55
56 // some versions of Moz do not support the readyState property
57 // and the onreadystate event so we patch it!
58 if (req.readyState == null) {
59 req.readyState = 1;
60 req.addEventListener("load", function () {
61 req.readyState = 4;
62 if (typeof req.onreadystatechange == "function")
63 req.onreadystatechange();
64 }, false);
65 }
66
67 return req;
68 }
69 if (window.ActiveXObject) {
70 return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
71 }
72 }
73 catch (ex) {}
74 // fell through
75 throw new Error("Your browser does not support XmlHttp objects");
76};
77
78// XmlDocument factory
79function XmlDocument() {}
80
81XmlDocument.create = function () {
82 try {
83 // DOM2
84 if (document.implementation && document.implementation.createDocument) {
85 var doc = document.implementation.createDocument("", "", null);
86
87 // some versions of Moz do not support the readyState property
88 // and the onreadystate event so we patch it!
89 if (doc.readyState == null) {
90 doc.readyState = 1;
91 doc.addEventListener("load", function () {
92 doc.readyState = 4;
93 if (typeof doc.onreadystatechange == "function")
94 doc.onreadystatechange();
95 }, false);
96 }
97
98 return doc;
99 }
100 if (window.ActiveXObject)
101 return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
102 }
103 catch (ex) {}
104 throw new Error("Your browser does not support XmlDocument objects");
105};
106
107// Create the loadXML method and xml getter for Mozilla
108if (window.DOMParser &&
109 window.XMLSerializer &&
110 window.Node && Node.prototype && Node.prototype.__defineGetter__) {
111
112 // XMLDocument did not extend the Document interface in some versions
113 // of Mozilla. Extend both!
114 //XMLDocument.prototype.loadXML =
115 Document.prototype.loadXML = function (s) {
116
117 // parse the string to a new doc
118 var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
119
120 // remove all initial children
121 while (this.hasChildNodes())
122 this.removeChild(this.lastChild);
123
124 // insert and import nodes
125 for (var i = 0; i < doc2.childNodes.length; i++) {
126 this.appendChild(this.importNode(doc2.childNodes[i], true));
127 }
128 };
129
130
131 /*
132 * xml getter
133 *
134 * This serializes the DOM tree to an XML String
135 *
136 * Usage: var sXml = oNode.xml
137 *
138 */
139 // XMLDocument did not extend the Document interface in some versions
140 // of Mozilla. Extend both!
141 /*
142 XMLDocument.prototype.__defineGetter__("xml", function () {
143 return (new XMLSerializer()).serializeToString(this);
144 });
145 */
146 Document.prototype.__defineGetter__("xml", function () {
147 return (new XMLSerializer()).serializeToString(this);
148 });
149}
Note: See TracBrowser for help on using the repository browser.