Posted on 2007-06-01 16:30
姜海龙 阅读(2355)
评论(0) 编辑 收藏
在我的项目中,系统要支持多语言环境,所以采用utf-8编码格式,系统菜单是通过一个action返回一个xml获得菜单数据进行加载的。
最初js脚本中采用如下方式进行加载:
1
var xmlDoc = new ActiveXObject("MSXML.DOMDocument");
2
xmlDoc.async = false;
3
xmlDoc.load("sysMemo.do");
结果返回的xml中文全是乱码,一直搞不明白是什么原因,该设置的地方也全部设置了。最后通过使用WebFx的一个js类通过xmlhttp的方式解决了这个问题。
下面这段代码是WebFx的xmlextras.js中的代码:
1
function getDomDocumentPrefix()
{
2
if (getDomDocumentPrefix.prefix)
3
return getDomDocumentPrefix.prefix;
4
5
var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
6
var o;
7
for (var i = 0; i < prefixes.length; i++)
{
8
try
{
9
// try to create the objects
10
o = new ActiveXObject(prefixes[i] + ".DomDocument");
11
return getDomDocumentPrefix.prefix = prefixes[i];
12
}
13
catch (ex)
{};
14
}
15
16
throw new Error("Could not find an installed XML parser");
17
}
18
19
function getXmlHttpPrefix()
{
20
if (getXmlHttpPrefix.prefix)
21
return getXmlHttpPrefix.prefix;
22
23
var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
24
var o;
25
for (var i = 0; i < prefixes.length; i++)
{
26
try
{
27
// try to create the objects
28
o = new ActiveXObject(prefixes[i] + ".XmlHttp");
29
return getXmlHttpPrefix.prefix = prefixes[i];
30
}
31
catch (ex)
{};
32
}
33
34
throw new Error("Could not find an installed XML parser");
35
}
36
37
//////////////////////////
38
// Start the Real stuff //
39
//////////////////////////
40
41
42
// XmlHttp factory
43
function XmlHttp()
{}
44
45
XmlHttp.create = function ()
{
46
try
{
47
if (window.XMLHttpRequest)
{
48
var req = new XMLHttpRequest();
49
50
// some versions of Moz do not support the readyState property
51
// and the onreadystate event so we patch it!
52
if (req.readyState == null)
{
53
req.readyState = 1;
54
req.addEventListener("load", function ()
{
55
req.readyState = 4;
56
if (typeof req.onreadystatechange == "function")
57
req.onreadystatechange();
58
}, false);
59
}
60
61
return req;
62
}
63
if (window.ActiveXObject)
{
64
return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
65
}
66
}
67
catch (ex)
{}
68
// fell through
69
throw new Error("Your browser does not support XmlHttp objects");
70
};
71
72
// XmlDocument factory
73
function XmlDocument()
{}
74
75
XmlDocument.create = function ()
{
76
try
{
77
// DOM2
78
if (document.implementation && document.implementation.createDocument)
{
79
var doc = document.implementation.createDocument("", "", null);
80
81
// some versions of Moz do not support the readyState property
82
// and the onreadystate event so we patch it!
83
if (doc.readyState == null)
{
84
doc.readyState = 1;
85
doc.addEventListener("load", function ()
{
86
doc.readyState = 4;
87
if (typeof doc.onreadystatechange == "function")
88
doc.onreadystatechange();
89
}, false);
90
}
91
92
return doc;
93
}
94
if (window.ActiveXObject)
95
return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
96
}
97
catch (ex)
{}
98
throw new Error("Your browser does not support XmlDocument objects");
99
};
100
101
// Create the loadXML method and xml getter for Mozilla
102
if (window.DOMParser &&
103
window.XMLSerializer &&
104
window.Node && Node.prototype && Node.prototype.__defineGetter__)
{
105
106
// XMLDocument did not extend the Document interface in some versions
107
// of Mozilla. Extend both!
108
XMLDocument.prototype.loadXML =
109
Document.prototype.loadXML = function (s)
{
110
111
// parse the string to a new doc
112
var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
113
114
// remove all initial children
115
while (this.hasChildNodes())
116
this.removeChild(this.lastChild);
117
118
// insert and import nodes
119
for (var i = 0; i < doc2.childNodes.length; i++)
{
120
this.appendChild(this.importNode(doc2.childNodes[i], true));
121
}
122
};
123
124
125
/**//*
126
* xml getter
127
*
128
* This serializes the DOM tree to an XML String
129
*
130
* Usage: var sXml = oNode.xml
131
*
132
*/
133
// XMLDocument did not extend the Document interface in some versions
134
// of Mozilla. Extend both!
135
XMLDocument.prototype.__defineGetter__("xml", function ()
{
136
return (new XMLSerializer()).serializeToString(this);
137
});
138
Document.prototype.__defineGetter__("xml", function ()
{
139
return (new XMLSerializer()).serializeToString(this);
140
});
141
}
下面是我调用的过程:
1
var xmlHttp = XmlHttp.create();
2
xmlHttp.open("GET", "showMenu.do", false); // async
3
xmlHttp.onreadystatechange = function ()
{
4
if (xmlHttp.readyState == 4)
{
5
getMenuTree(xmlHttp.responseXML);
6
}
7
};
8
xmlHttp.send(null);
这段代码要包含在一个方法中。