下面是区别IE6、IE7、IE8之间的方法:
var isIE=!!window.ActiveXObject;
var isIE6=isIE&&!window.XMLHttpRequest;
var isIE8=isIE&&!!document.documentMode;
var isIE7=isIE&&!isIE6&&!isIE8;
if (isIE){
if (isIE6){
alert(”ie6″);
}else if (isIE8){
alert(”ie8″);
}else if (isIE7){
alert(”ie7″);
}
}
function getXmlHttpRequest(){
var xhr = null;
if (window.ActiveXObject) {
xhr = new ActiveObject("Microsoft.XMLHTTP“);
}else if(window.XMLHTTPRequest) { //非IE browser 存在XMLHTTPRequest js对象
xhr = new window.XMLHTTPRequest();
}
return xhr;
}
xhr = getXmlHttpRequest(); xhr.readyState=0; Uninitialized 初始化状态;
//设置头对象
xhr.open(method, url,async[,user, pass]); //method:get, post,head, async:true,false; 调用完readyState =1
xhr.setRequestHeader(name,value); //可以设置表头,name不能含空白冒号换行,在open之后,send之前,如果已有header,会在后面追加。源内容, name=value
xhr.send(body); 当为post时,传递的参数体 ,当为get时, body=null,完成后状态为sent
xhr.onreadystatechange=function(){ //readystate状态变化触发
if (xhr.readyState ==3){ //表示receiving 正在接收状态
}if(xhr.readyState ==4){ //表示loaded接收完成
if (xhr.status ==200){ //表示成功返回 404没有找到 ; xhr.statusText 返回状态对应文本
document.getElementById("result").innerHtml=xhr.responseText //返回文本;同时还返回 xhr.responseXML
}
}
}
本文介绍了如何通过JavaScript区分不同的Internet Explorer版本,并提供了实现Ajax请求的getXmlHttpRequest函数示例。该方法适用于IE6、IE7和IE8浏览器。
143

被折叠的 条评论
为什么被折叠?



