flymolgee
/**
 * @description 事件绑定,兼容各浏览器
 * @param target 事件触发对象 
 * @param type   事件
 * @param func   事件处理函数
 */
function addEvents(target, type, func) {
    if (target.addEventListener)    //非ie 和ie9
        target.addEventListener(type, func, false);
    else if (target.attachEvent)   //ie6到ie8
        target.attachEvent("on" + type, func);
    else target["on" + type] = func;   //ie5
};
/**
 * @description 事件移除,兼容各浏览器
 * @param target 事件触发对象
 * @param type   事件
 * @param func   事件处理函数
 */
function removeEvents(target, type, func){
    if (target.removeEventListener)
        target.removeEventListener(type, func, false);
    else if (target.detachEvent)
        target.detachEvent("on" + type, func);
    else target["on" + type] = null;
};

 

        var XHR=null;
        if (window.XMLHttpRequest) {
            // 非IE内核
            XHR = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            // IE内核,这里早期IE的版本写法不同,具体可以查询下
            XHR = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            XHR = null;
        }

        if(XHR){
            XHR.open("GET", "ajaxServer.action");

            XHR.onreadystatechange = function () {
                // readyState值说明
                // 0,初始化,XHR对象已经创建,还未执行open
                // 1,载入,已经调用open方法,但是还没发送请求
                // 2,载入完成,请求已经发送完成
                // 3,交互,可以接收到部分数据

                // status值说明
                // 200:成功
                // 404:没有发现文件、查询或URl
                // 500:服务器产生内部错误
                if (XHR.readyState == 4 && XHR.status == 200) {
                    // 这里可以对返回的内容做处理
                    // 一般会返回JSON或XML数据格式
                    console.log(XHR.responseText);
                    // 主动释放,JS本身也会回收的
                    XHR = null;
                }
            };
            XHR.send();
        }

 

分类:

技术点:

相关文章:

  • 2021-09-13
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2022-02-07
猜你喜欢
  • 2022-02-07
  • 2022-12-23
  • 2022-02-07
  • 2022-02-09
  • 2022-02-09
  • 2022-02-09
  • 2022-02-07
相关资源
相似解决方案