-
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://www.mryang.org/logs/16101540.html
在很多页面中都可能会用到同样的Ajax初始化代码,因此,可以把这部分公共的代码写到一个js文件中, 在某个页面需要调用时,直接调用这个文件就可以了,就不要在每个页面中都加入一段Ajax的初始化代码。这段公用代码如下:
var xmlHttp;
function createXMLHttpRequest() {
xmlHttp = false;
if(window.XMLHttpRequest) { //Mozilla
xmlHttp = new XMLHttpRequest();
if (xmlHttp.overrideMimeType) {
xmlHttp.overrideMimeType(”text/xml”);
}
}
else if (window.ActiveXObject) { // IE
try {
xmlHttp = new ActiveXObject(”Msxml2.XMLHTTP”);
} catch (e) {
try {
xmlHttp = new ActiveXObject(”Microsoft.XMLHTTP”);
} catch (e) {}
}
}
if (!xmlHttp) { // 异常
window.alert(”不能创建XMLHttpRequest对象实例.”);
return false;
}}
function processRequest() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
document.getElementById(”showresult”).innerHTML = xmlHttp.responseText;
} else {
alert(”您所请求的页面有异常。”);
}
}
}function startRequest(strurl){
createXMLHttpRequest();
xmlHttp.onreadystatechange = processRequest;
xmlHttp.open(”Get”, strurl, true);
xmlHttp.send(null);
}可以将这段代码存为ajaxcommon.js文件,在页
中需要使用时在标签中引入就可以了,代码如下:
< language=”JavaScript” type=”text/javascript” src=”http://mryang.blogspot.com/js/ajaxcommon.js”>< /script>在使用的页面中用下面的方法来调用:
function resultshow() {
var temstr=”showmsg.asp”;
document.getElementById(”showresult”).parentNode.style.display = “”;
document.getElementById(”showresult”).innerHTML = “正在读取数据…”;
startRequest(temstr);
}随机文章:
Ubuntu 7.10 Server上配置较完整的Web Server 2008-04-22Vista中IIS的权限问题 2008-03-10痛苦的网站搬家 2007-06-26给Google Personalized Home换Skin 2007-04-18第一次使用Adobe Premiere Pro 2006-05-23
收藏到:Del.icio.us