📄 html_ajax.js
字号:
if (this._timeout) { window.clearTimeout(this._timeout); } this.readyState = 1; if (typeof(this.onreadystatechange) == "function") { this.onreadystatechange(); } }, // This will send all headers in this._response and will include lastModified and contentType if not already set getAllResponseHeaders: function() { var string = ''; for (i in this._response) { string += i + ' : ' + this._response[i] + "\n"; } return string; }, // This will use lastModified and contentType if they're not set getResponseHeader: function(header) { return (this._response[header] ? this._response[header] : null); }, // Assigns a label/value pair to the header to be sent with a request setRequestHeader: function(label, value) { this._headers[label] = value; return; }, // Assigns destination URL, method, and other optional attributes of a pending request open: function(method, url, async, username, password) { if (!document.body) { throw('CANNOT_OPEN_SEND_IN_DOCUMENT_HEAD'); } //exceptions for not enough arguments if (!method || !url) { throw('NOT_ENOUGH_ARGUMENTS:METHOD_URL_REQUIRED'); } //get and post are only methods accepted this._method = (method.toUpperCase() == 'POST' ? 'POST' : 'GET'); this._decodeUrl(url); this._async = async; if(!this._async && document.readyState && !window.opera) { throw('IE_DOES_NOT_SUPPORT_SYNC_WITH_IFRAMEXHR'); } //set status to loading and call onreadystatechange this.readyState = 1; if(typeof(this.onreadystatechange) == "function") { this.onreadystatechange(); } }, // Transmits the request, optionally with postable string or DOM object data send: function(content) { //attempt opera history munging if (window.opera) { this._history = window.history.length; } //create a "form" for the contents of the iframe var form = '<html><body><form method="' + (this._url.indexOf('px=') < 0 ? this._method : 'post') + '" action="' + this._url + '">'; //tell iframe unwrapper this IS an iframe form += '<input name="Iframe_XHR" value="1" />'; //class and method if (this._phpclass != null) { form += '<input name="Iframe_XHR_class" value="' + this._phpclass + '" />'; } if (this._phpmethod != null) { form += '<input name="Iframe_XHR_method" value="' + this._phpmethod + '" />'; } // fake headers for (label in this._headers) { form += '<textarea name="Iframe_XHR_headers[]">' + label +':'+ this._headers[label] + '</textarea>'; } // add id form += '<textarea name="Iframe_XHR_id">' + this._id + '</textarea>'; if (content != null && content.length > 0) { form += '<textarea name="Iframe_XHR_data">' + content + '</textarea>'; } form += '<input name="Iframe_XHR_HTTP_method" value="' + this._method + '" />'; form += '<s'+'cript>document.forms[0].submit();</s'+'cript></form></body></html>'; form = "javascript:document.write('" + form.replace(/\'/g,"\\'") + "');void(0);"; this.readyState = 2; if (typeof(this.onreadystatechange) == "function") { this.onreadystatechange(); } // try to create an iframe with createElement and append node try { var iframe = document.createElement('iframe'); iframe.id = this._id; // display: none will fail on some browsers iframe.style.visibility = 'hidden'; // for old browsers with crappy css iframe.style.border = '0'; iframe.style.width = '0'; iframe.style.height = '0'; if (document.all) { // MSIE, opera iframe.src = form; document.body.appendChild(iframe); } else { document.body.appendChild(iframe); iframe.src = form; } } catch(exception) { // dom failed, write the sucker manually var html = '<iframe src="' + form +'" id="' + this._id + '" style="visibility:hidden;border:0;height:0;width:0;"></iframe>'; document.body.innerHTML += html; } if (this._async == true) { //avoid race state if onload is called first if (this.readyState < 3) { this.readyState = 3; if(typeof(this.onreadystatechange) == "function") { this.onreadystatechange(); } } } else { //we force a while loop for sync, it's ugly but hopefully it works while (this.readyState != 4) { //just check to see if we can up readyState if (this.readyState < 3) { this.readyState = 3; if(typeof(this.onreadystatechange) == "function") { this.onreadystatechange(); } } } } }, // attached as an onload function to the iframe to trigger when we're done isLoaded: function(headers, data) { this.readyState = 4; //set responseText, Status, StatusText this.status = 200; this.statusText = 'OK'; this.responseText = data; this._response = headers; if (!this._response['Last-Modified']) { string += 'Last-Modified : ' + document.getElementById(this._id).lastModified + "\n"; } if (!this._response['Content-Type']) { string += 'Content-Type : ' + document.getElementById(this._id).contentType + "\n"; } // if this is xml populate responseXML accordingly if (this._response['Content-Type'] == 'application/xml') { return new DOMParser().parseFromString(this.responseText, 'application/xml'); } //attempt opera history munging in opera 8+ - this is a REGRESSION IN OPERA if (window.opera && window.opera.version) { //go back current history - old history window.history.go(this._history - window.history.length); } if (typeof(this.onreadystatechange) == "function") { this.onreadystatechange(); } document.body.removeChild(document.getElementById(this._id)); }, // strip off the c and m from the url send...yuck _decodeUrl: function(querystring) { //opera 7 is too stupid to do a relative url...go figure var url = unescape(location.href); url = url.substring(0, url.lastIndexOf("/") + 1); var item = querystring.split('?'); //rip off any path info and append to path above <- relative paths (../) WILL screw this this._url = url + item[0].substring(item[0].lastIndexOf("/") + 1,item[0].length); if(item[1]) { item = item[1].split('&'); for (i in item) { var v = item[i].split('='); if (v[0] == 'c') { this._phpclass = v[1]; } else if (v[0] == 'm') { this._phpmethod = v[1]; } } } if (!this._phpclass || !this._phpmethod) { var cloc = window.location.href; this._url = cloc + (cloc.indexOf('?') >= 0 ? '&' : '?') + 'px=' + escape(HTML_AJAX_Util.absoluteURL(querystring)); } }}// serializer/UrlSerializer.js// {{{ HTML_AJAX_Serialize_Urlencoded/** * URL-encoding serializer * * This class can be used to serialize and unserialize data in a * format compatible with PHP's handling of HTTP query strings. * Due to limitations of the format, all input is serialized as an * array or a string. See examples/serialize.url.examples.php * * @version 0.0.1 * @copyright 2005 Arpad Ray <arpad@php.net> * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * * See Main.js for Author/license details */function HTML_AJAX_Serialize_Urlencoded() {}HTML_AJAX_Serialize_Urlencoded.prototype = { contentType: 'application/x-www-form-urlencoded; charset=UTF-8', base: '_HTML_AJAX', _keys: [], error: false, message: "", cont: "", // {{{ serialize /** * Serializes a variable * * @param mixed inp the variable to serialize * @return string a string representation of the input, * which can be reconstructed by unserialize() */ serialize: function(input, _internal) { if (typeof input == 'undefined') { return ''; } if (!_internal) { this._keys = []; } var ret = '', first = true; for (i = 0; i < this._keys.length; i++) { ret += (first ? HTML_AJAX_Util.encodeUrl(this._keys[i]) : '[' + HTML_AJAX_Util.encodeUrl(this._keys[i]) + ']'); first = false; } ret += '='; switch (HTML_AJAX_Util.getType(input)) { case 'string': case 'number': ret += HTML_AJAX_Util.encodeUrl(input.toString()); break; case 'boolean': ret += (input ? '1' : '0'); break; case 'array': case 'object': ret = ''; for (i in input) { this._keys.push(i); ret += this.serialize(input[i], true) + '&'; this._keys.pop(); } ret = ret.substr(0, ret.length - 1); } return ret; }, // }}} // {{{ unserialize /** * Reconstructs a serialized variable * * @param string inp the string to reconstruct * @return array an array containing the variable represented by the input string, or void on failure */ unserialize: function(input) { if (!input.length || input.length == 0) { // null return; } if (!/^(\w+(\[[^\[\]]*\])*=[^&]*(&|$))+$/.test(input)) { this.raiseError("invalidly formed input", input); return; } input = input.split("&"); var pos, key, keys, val, _HTML_AJAX = []; if (input.length == 1) { return HTML_AJAX_Util.decodeUrl(input[0].substr(this.base.length + 1)); } for (var i in input) { pos = input[i].indexOf("="); if (pos < 1 || input[i].length - pos - 1 < 1) { this.raiseError("input is too short", input[i]); return; } key = HTML_AJAX_Util.decodeUrl(input[i].substr(0, pos)); val = HTML_AJAX_Util.decodeUrl(input[i].substr(pos + 1)); key = key.replace(/\[((\d*\D+)+)\]/g, '["$1"]'); keys = key.split(']'); for (j in keys) { if (!keys[j].length || keys[j].length == 0) { continue; } try { if (eval('typeof ' + keys[j] + ']') == 'undefined') { var ev = keys[j] + ']=[];'; eval(ev); } } catch (e) { this.raiseError("error evaluating key", ev); return; } } try { eval(key + '="' + val + '";'); } catch (e) { this.raiseError("error evaluating value", input); return; } } return _HTML_AJAX; }, // }}} // {{{ getError /** * Gets the last error message * * @return string the last error message from unserialize() */ getError: function() { return this.message + "\n" + this.cont; }, // }}} // {{{ raiseError /** * Raises an eror (called by unserialize().) * * @param string message the error message * @param string cont the remaining unserialized content */ raiseError: function(message, cont) { this.error = 1; this.message = message; this.cont = cont; } // }}}}// }}}// serializer/phpSerializer.js// {{{ HTML_AJAX_Serialize_PHP/** * PHP serializer * * This class can be used to serialize and unserialize data in a * format compatible with PHP's native serialization functions. * * @version 0.0.3 * @copyright 2005 Arpad Ray <arpad@php.net> * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * * See Main.js for Author/license details */function HTML_AJAX_Serialize_PHP() {}HTML_AJAX_Serialize_PHP.prototype = { error: false, message: "", cont: "", defaultEncoding: 'UTF-8', contentType: 'application/php-serialized; charset: UTF-8', // {{{ serialize /** * Serializes a variable * * @param mixed inp the variable to serialize * @return string a string representation of the input, * which can be reconstructed by unserialize() * @author Arpad Ray <arpad@rajeczy.com> * @author David Coallier <davidc@php.net> */ serialize: function(inp) { var type = HTML_AJAX_Util.getType(inp); var val; switch (type) { case "undefined": val = "N"; break; case "boolean": val = "b:" + (inp ? "1" : "0"); break; case "number": val = (Math.round(inp) == inp ? "i" : "d") + ":" + inp; break; case "string": val = "s:" + inp.length + ":\"" + inp + "\""; break; case "array": val = "a"; case "object": if (type == "object") { var objname = inp.constructor.toString().match(/(\w+)\(\)/); if (objname == undefined) { return; } objname[1] = this.serialize(objname[1]); val = "O" + objname[1].substring(1, objname[1].length - 1); } var count = 0; var vals = ""; var okey; for (key in inp) { okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key); vals += this.serialize(okey) + this.serialize(inp[key]); count++; } val += ":" + count + ":{" + vals + "}"; break; } if (type != "object" && type != "array") val += ";"; return val; }, // }}} // {{{ unserialize /** * Reconstructs a serialized variable * * @param string inp the string to reconstruct * @return mixed the variable represented by the input string, or void on failure */ unserialize: function(inp) { this.error = 0; if (inp == "" || inp.length < 2) { this.raiseError("input is too short"); return; } var val, kret, vret, cval; var type = inp.charAt(0); var cont = inp.substring(2); var size = 0, divpos = 0, endcont = 0, rest = "", next = ""; switch (type) { case "N": // null if (inp.charAt(1) != ";") { this.raiseError("missing ; for null", cont); } // leave val undefined rest = cont; break; case "b": // boolean if (!/[01];/.test(cont.substring(0,2))) { this.raiseError("value not 0 or 1, or missing ; for boolean", cont); } val = (cont.charAt(0) == "1"); rest = cont.substring(1); break; case "s": // string val = ""; divpos = cont.indexOf(":"); if (divpos == -1) { this.raiseError("missing : for string", cont); break; } size = parseInt(cont.substring(0, divpos)); if (size == 0) { if (cont.length - divpos < 4) { this.raiseError("string is too short", cont); break; } rest = cont.substring(divpos + 4); break; } if ((cont.length - divpos - size) < 4) { this.raiseError("string is too short", cont); break; } if (cont.substring(divpos + 2 + size, divpos + 4 + size) != "\";") { this.raiseError("string is too long, or missing \";", cont); } val = cont.substring(divpos + 2, divpos + 2 + size); rest = cont.substring(divpos + 4 + size); break; case "i": // integer case "d": // float var dotfound = 0; for (var i = 0; i < cont.length; i++) { cval = cont.charAt(i); if (isNaN(parseInt(cval)) && !(type == "d" && cval == "." && !dotfound++)) { endcont = i; break; } } if (!endcont || cont.charAt(endcont) != ";") { this.raiseError("missing or invalid value, or missing ; for int/float", cont); } val = cont.substring(0, endcont); val = (type == "i" ? parseInt(val) : parseFloat(val)); rest = cont.substring(endcont + 1); break; case "a": // array if (cont.length < 4) { this.raiseError("array is too short", cont);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -