hightlight代码高亮框架在cnblogs上进行测试

var ajax ={
    // XMLHttpRequest 对象
    xhr : function( ) {
        return window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    },

    // -串行化一列数据。支持4种不同的对象;
    // -若是字符串或 null 就直接将其返回
    // -表单输入的元素的数组
    // -键/值对的散列表
    // -返回值为串行后的字符串
    serialize : function( a ){
        var s = [];
        // 若传入的参数是数组,假定他们是表单元素的数组
        if( typeof a === "string" || a===null || a===undefined ) {
            return a;
        } else if( a.constructor === undefined || a.constructor == Array ) { //串行化表单元素
            // 一下的串行化表单元素的浏览器兼容性还没解决
            return ;
            for( var i = 0, a=KissC.Tools.makeArray(a); i < a.length; i++){
                s.push( a[i].name + "=" + encodeURIComponent( a[i].value) );
            }
        } else {// 否则,假设这个为键值对象
            for(var j in a) {
                s.push( j + "=" + encodeURIComponent( a[j] ) );
            }
        }
        return s.join( "&" );
    },

    // 服务器状态判断
    httpSuccess : function(r) {
        try {
            //alert( location.protocol == "file:" );
            // 如果得不到服务器状态,且我们在请求本地文件,设为成功
            return !r.status && location.protocol == "file:" || 
            // 所有 200 - 300 之间的状态码都表示成功
            ( r.status >= 200 && r.status < 300 ) ||
            // 文档未作修改设为成功
            r.status == 304 ||
            // Safari 在文档未修改时返回空状态
            navigator.userAgent.indexOf("Safari") >= 0 
                && typeof r.status == "undefined";
        }catch (e){}
        // 若检查状态失败,就设置请求为失败
        return false;
    },

    // 服务器返回数据类型检测
    httpDataType : function( r, type ){
        // 获取content-type的首部
        var ct = r.getResponseHeader("content-type");
        // 若没有设置默认的类型,判断服务器返回的是否是 XML 形式
        var data = !type && ct && ct.indexOf("xml") >= 0;
        // 若是获得 XML 文档对象,否则返回文本内容
        data = type == "xml" || data ? r.responseXML : r.responseText;
        // 若指定类型是"script",则以javascript形势返回为本
        if( type === "script")
            eval.call( window, data );
        return data;
    },

    send : function( options ){
        var xhr, requestDone, errorText;
        // 设置 options 的默认值
        options = {
            // HTTP 请求的类型
            method : options.method ? options.method.toUpperCase() : "POST",
            // 请求的 URL
            url : options.url || "",
            // 请求的超时时间
            timeout : options.timeout || 5000,
            // 请求 失败,成功,完成时执行的函数(不管是否成功还是失败都会调用)
            onComplete : options.onComplete || function(){},
            onError : options.onError || function(){},
            onSuccess : options.onSuccess || function(){},
            // 服务器将返回的数据类型,这个默认值用于判断服务器返回的数据类型,并作相应动作
            data : ajax.serialize( options.data ) || "",
            dataType : options.dataType || "HTML",
            // 异步还是同步,默认是异步(true)
            async : null == options.async ? true : Boolean(options.async),
            username : options.username || "",
            password : options.password || "",
            cache : options.cache || true
        };
        // 创建请求对象
        xhr = new ajax.xhr();
        //初始化一个 5 秒后执行的回调函数,如果请求尚未完成的话,将请求取消
        setTimeout( function() {
            requestDone = true;
        }, options.timeout );
        // 文档状态监听函数,在文档的状态改变时被调用
        xhr.onreadystatechange = function() {
            // 保持等待,直到数据完全加载,并保证请求并未超时
            if( xhr.readyState==4 && !requestDone ) {
                if( ajax.httpSuccess( xhr ) ) {
                    //以服务器返回的数据作为参数调用成功回调函数
                    options.onSuccess( ajax.httpDataType( xhr, options.dataType ) );
                } else {
                    errorText = xhr.status == "404" ? "Not Found" : "Unknown Error";
                    options.onError.call( this, errorText);
                }
                //调用完成回调函数
                options.onComplete();
                // 避免内存泄漏,清理文档
                xhr = null;
            } else if ( requestDone == true && xhr.readyState==3 ){
                errorText = "请求超时";
                options.onError.call( this, errorText );
            }
        };
        // 初始化异步 method 的请求
        xhr.open( options.method, options.url, options.async ,options.username, options.password );
        // 设置 Content-type 首部,告知服务器如何解析我们发送的数据
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        //xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');//告知是ajax请求
        // 保证浏览器发送的串行化数据长度正确
        // 基于 Mozilla 的浏览器有时处理这个会碰到问题
        //if( xhr.overrideMimeType)
            //xhr.setRequestHeader("Connection","close");
        // 与服务器建立连接
        xhr.send(options.data);
        return xhr;
    }

};

框架官网:http://softwaremaniacs.org/soft/highlight/en/

posted @   nodejs  阅读(154)  评论(0)    收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示