⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taconite-client.js

📁 买书附带的光盘资料Foundations_Of_Ajax中文版教程及源代码。 非常好的学习Ajax的教程
💻 JS
字号:
/**    @fileoverview    This JavaScript file represents the core browser-side functionality    supplied by Taconite. In general, the tools in this file wrap an instance    of XMLHttpRequest object and provide utility methods for gather data from    form elements to be sent to the server as par of an Ajax request.*//**    Constructor for the AjaxRequest class.     <br><br>    Example:    <br><br>    var ajaxRequest = new AjaxRequest("YOUR_URL");    @class The AjaxRequest object wraps an instance of XMLHttpRequest and provides     facilities for setting functions that are called before a request is made    and after a request returns. By default, AjaxRequest handles the server    response by simply calling eval(), passing to it the responseText from     the XMLHttpRequestObject, of course assuming that the response was     generated by Taconite on the server side and that running eval() will     update the web page.<br><br>Example Usage:<br><br>var ajaxRequest = new AjaxRequest("YOUR_URL");    <br>ajaxRequest.addFormElements("form_element_id_attribute_value");    <br>ajaxRequest.sendRequest();    @constructor    @param {String} a String repesenting the URL to which the Ajax request    will be sent.*/function AjaxRequest(url) {    /** @private */    var self = this;    /** @private */    var xmlHttp = createXMLHttpRequest();        /** @private */    var queryString = "";    /** @private */    var requestURL = url;    /** @private */    var method = "GET";    /** @private */    var preRequest = null;    /** @private */    var postRequest = null;    /**        Return the instance of the XMLHttpRequest object wrapped by this object.        @return XMLHttpRequest    */    this.getXMLHttpRequestObject = function() {        return xmlHttp;    }    /**        Set the pre-request function. This function will be called prior to         sending the Ajax request. The pre-request function is passed a reference        to this object.        @param {Function} The function to be called prior to sending the Ajax        request. The function is passed a refernce of this object.    */    this.setPreRequest = function(func) {        preRequest = func;    }    /**        Set the post-request function. This function will be called after the        response has been received and after eval() has been called using the         XMLHttpRequest object's responseText. The post-request function is passed         a reference to this object.        @param {Function} The function to be called after receiving the Ajax        response. The function is passed a refernce of this object.    */    this.setPostRequest = function(func) {        postRequest = func;    }    /**        Send the Ajax request using the POST method. Use with caution -- some        browsers do not support the POST method with the XMLHttpRequest object.    */    this.setUsePOST = function() {        method = "POST";    }    /**        Send the Ajax request using the GET method, where parameters are sent        as a query string appended to the URL. This is the default behavior.    */    this.setUseGET = function() {        method = "GET";    }    /**        Add all of the form elements under the specified form to the query        string to be sent to the server as part of the Ajax request. The values        are automatically encoded.        @param {String} formID, the value of the id attribute of the form from        which you wish to accumulate the form values.    */    this.addFormElements = function(formID) {        var formElements = document.getElementById(formID).elements;        var values = toQueryString(formElements);        accumulateQueryString(values);    }    /** @private */    function accumulateQueryString(newValues) {        if(queryString == "") {            queryString = newValues;         }        else {            queryString = this.queryString + "&" +  newValues;        }    }    /**        Add the values of the named form elements to the query string to be        sent to the server as part of the Ajax request. This method takes any         number of Strings representing the form elements for wish you wish to         accumulate the values. The Strings must be the value of the element's         name attribute.<br><br>For example, these are all valid uses:<br>        <br>ajaxRequest.addNamedFormElements("element-name-1");        <br>ajaxRequest.addNamedFormElements("element-name-1", "element-name-2", "element-name-3");    */    this.addNamedFormElements = function() {        var elementName = "";        var namedElements = null;        for(var i = 0; i < arguments.length; i++) {            elementName = arguments[i];            namedElements = document.getElementsByName(elementName);            elementValues = toQueryString(namedElements);            accumulateQueryString(elementValues);        }    }    /**        Add the values of the id'd form elements to the query string to be        sent to the server as part of the Ajax request. This method takes any         number of Strings representing the ids of the form elements for wish you wish to         accumulate the values. The Strings must be the value of the element's         name attribute.<br><br>For example, these are all valid uses:<br>        <br>ajaxRequest.addFormElementsById("element-id-1");        <br>ajaxRequest.addFormElementsById("element-id-1", "element-id-2", "element-id-3");    */    this.addFormElementsById = function() {        var id = "";        var element = null;        var elements = new Array();        for(var h = 0; h < arguments.length; h++) {            element = document.getElementById(arguments[h]);            if(element != null) {                elements[h] = element;            }        }        elementValues = toQueryString(elements);        accumulateQueryString(elementValues);    }    /**        Send the Ajax request.    */    this.sendRequest = function() {        if(preRequest) {            preRequest(self);        }        var obj = this;        xmlHttp.onreadystatechange = function () { handleStateChange(self) };        requestURL = requestURL + "?ts=" + new Date().getTime();        if(method == "GET") {            requestURL = requestURL + "&" + queryString;            xmlHttp.open(method, requestURL, true);            xmlHttp.send(null);        }        else {            xmlHttp.open(method, requestURL, true);            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");             xmlHttp.send(queryString);        }    }    handleStateChange = function(ajaxRequest) {        if(ajaxRequest.getXMLHttpRequestObject().readyState != 4) {            return;        }        if(ajaxRequest.getXMLHttpRequestObject().status == 200) {            var nodes = ajaxRequest.getXMLHttpRequestObject().responseXML.documentElement.childNodes;            var parser = null;            var parseInBrowser = "";            for(var i = 0; i < nodes.length; i++) {                if(nodes[i].nodeType != 1) {                    continue;                }                parseInBrowser = nodes[i].getAttribute("parseInBrowser");                if(parseInBrowser == "true") {                    parser = new XhtmlToDOMParser(nodes[i]);                    parser.startParsing();                    var js = parser.getJavaScript();                    eval(parser.getJavaScript());                }                else {                    eval(nodes[i].firstChild.nodeValue);                }            }            if(postRequest) {                postRequest(ajaxRequest);            }                }    }    /** @private */    function toQueryString(elements) {        var node = null;        var qs = "";        var name = "";        var tempString = "";        for(var i = 0; i < elements.length; i++) {            tempString = "";            node = elements[i];            name = node.getAttribute("name");            if(node.tagName.toLowerCase() == "input") {                if(node.type.toLowerCase() == "radio" || node.type.toLowerCase() == "checkbox") {                    if(node.checked) {                        tempString = name + "=" + node.value;                    }                }                if(node.type.toLowerCase() == "text" || node.type.toLowerCase() == "hidden") {                    tempString = name + "=" + encodeURIComponent(node.value);                }            }            else if(node.tagName.toLowerCase() == "select") {                tempString = getSelectedOptions(node);            }            else if(node.tagName.toLowerCase() == "textarea") {                tempString = name + "=" + encodeURIComponent(node.value);            }            if(tempString != "") {                if(qs == "") {                    qs = tempString;                }                else {                    qs = qs + "&" + tempString;                }            }        }        return qs;    }    /** @private */    function getSelectedOptions(select) {        var options = select.options;        var option = null;        var qs = "";        var tempString = "";        for(var x = 0; x < options.length; x++) {            tempString = "";            option = options[x];            if(option.selected) {                tempString = select.name + "=" + option.value;            }            if(tempString != "") {                if(qs == "") {                    qs = tempString;                }                else {                    qs = qs + "&" + tempString;                }            }        }        return qs;    }}/**    Create an instance of the XMLHttpRequest object, using the appropriate    method for the type of browser in which this script is running. For Internet    Explorer, it's an ActiveX object, for all others it's a native JavaScript    object.    @return an instance of the XMLHttpRequest object.*/function createXMLHttpRequest() {    if (window.ActiveXObject) {        return new ActiveXObject("Microsoft.XMLHTTP");    }    else if (window.XMLHttpRequest) {        return new XMLHttpRequest();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -