Ajax的完整兼容各种浏览器版本代码

<script type="text/javascript">
function createAjax(){
var request=false;

//window对象中有XMLHttpRequest存在就是非IE,有可能是(IE7,IE8)
if(window.XMLHttpRequest){
request=new XMLHttpRequest();

if(request.overrideMimeType){
request.overrideMimeType("text/xml");
}


//window对象中有ActiveXObject属性存在就是IE中的Microsoft.XMLHTTP代表不同IE版本的参数
        }else if(window.ActiveXObject){
//ActiveXOject('Microsoft.XMLHTTP')
var versions=['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];

for(var i=0; i<versions.length; i++){
try{
request=new ActiveXObject(versions[i]);

if(request){
return request;
}
}catch(e){
request=false;
}
}
}
return request;
}

var ajax=createAjax();

alert(ajax);
</script>

posted on 2017-02-10 10:24  shenzen_小白  阅读(2124)  评论(0编辑  收藏  举报

导航