以下世xmlHttp对象产生和提交的一个例子:
xmlHttp.js
var xmlhttp,alerted;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
} catch (e) {
try {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
} catch (E) {
alert("You must have Microsofts XML parsers available")
}
}
@else
alert("You must have JScript version 5 or above.")
xmlhttp=false
alerted=true
@end @*/
if (!xmlhttp && !alerted) {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
alert("You need a browser which supports an XMLHttpRequest Object./nMozilla build 0.9.5 has this Object and IE5 and above, others may do, I don't know, any info jim@jibbering.com")
}
}
function RSchange(){
if (xmlhttp.readyState==4) {
//alert(xmlhttp.responseText);
}
}
function submit(strUrl) {
if (xmlhttp) {
xmlhttp.open("post", strUrl,true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange=RSchange;
xmlhttp.send(null);
}
}
在jsp或者其他网页的form表单提交时,在js中使用:
submit("/project/servlet?param1="+URLEncode(p1)+"&param2="+p2);
转码:
function URLEncode(fld)
{
if (fld == "") return "";
var encodedField = "";
var s = fld;
if (typeof encodeURIComponent == "function")
{
encodedField = encodeURIComponent(s);
}
else
{
encodedField = encodeURIComponentNew(s);
}
return encodedField;
}