js获取XMLHttpRequest对象的工具类

最近项目中需要用到js页面跳转,因此需要用到XMLHTTPRequest对象,所以就用学习一波,分享给大家。

/**
         * 获取XMLHttpRequest对象
         */
        function getXHR(){
            var xhr = null;
            // 浏览器支持 XMLHttpRequest
            if(window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                // IE6 以下浏览器只支持 AactiveXObject
                try {
                    // 获取 MSXML3 标准
                    xhr = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try {
                        // 备选方案,功能不完善,不推荐使用
                        xhr = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {
                        alert("您的浏览器暂不支持Ajax!");
                    }
                }
            }
            return xhr;
        }
发布了39 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/a1920135141/article/details/99303899