您的位置:首页 > Web前端 > JavaScript

动态网页中javascript的异步提交实现

2005-04-28 09:31 549 查看
以下世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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息