viewing source for "expand-comments.js"

last commit

commit ec9726ece091495bad03d1d5096e468fbba6ca06 Author: andy <andy@a04a6e75-5819-0410-a511-8dafea38c924> Date: Thu Mar 22 13:42:47 2007 +0000
Added 1.3.1 info

source code

001: /* Expand Comments version 1.3.1
002:  *  Copyright 2006 Andrew Rader
003:  *
004:  * This file is part of Expand Comments
005:  *
006:  * Expand Comments is free software; you can redistribute it and/or modify
007:  * it under the terms of the GNU General Public License as published by
008:  * the Free Software Foundation; either version 2 of the License, or
009:  * (at your option) any later version.
010: 
011:  * Expand Comments is distributed in the hope that it will be useful,
012:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014:  * GNU General Public License for more details.
015:  *
016:  * You should have received a copy of the GNU General Public License
017:  * along with Expand Comments; if not, write to the Free Software
018:  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019:  */
020: 
021: // buildAjaxRequest is inspired by www.sitepoint.com/article/take-command-ajax
022: // which is taken from other various tuts. Sharing is caring.
023: // For documentation's sake, callback is a function that takes
024: // a single argument. This argument holds what is returned from
025: // the server, as XML of retXML is true, or as Text if not
026: // buildAjaxRequest will also look to see if there is a third
027: // argument, which should be an array of more arguments.
028: // This array is expanded, and when the callback is called,
029: // It will be given this list of arguments
030: 
031: function buildAjaxRequest( callback, retXML, ignoreStatus ) {
032:     var http_request = false;
033: 
034:     if (window.XMLHttpRequest) { // Mozilla, Safari,...
035:         http_request = new XMLHttpRequest();
036:         if (http_request.overrideMimeType) {
037:             http_request.overrideMimeType('text/xml');
038:         }
039:     }
040:     else if (window.ActiveXObject) { // IE
041:         try {
042:             http_request = new ActiveXObject("Msxml2.XMLHTTP");
043:         } catch (e) {
044:             try {
045:                 http_request = new ActiveXObject("Microsoft.XMLHTTP");
046:             } catch (e) {}
047:         }
048:     }
049: 
050:     if( http_request ) {
051: 
052:         if( retXML ) {
053:             evalString = '(http_request.responseXML';
054:         }
055:         else {
056:             evalString = '(http_request.responseText';
057:         }
058: 
059:         if( arguments.length == 4 ) {
060:             args = arguments[3];
061: 
062:             for( var i = 0; i < args.length; i++ ) {
063:                 evalString += (',args[' + i + ']');
064:             }
065:         }
066: 
067:         evalString += ')';
068: 
069:         http_request.onreadystatechange = function() {
070:             if (http_request.readyState == 4) {
071:                 if( ignoreStatus || http_request.status == 200 ) {
072:                     eval(callback + evalString);
073:                 }
074:             }
075:         }
076:     }
077: 
078:     return http_request;
079: }
080: 
081: /*
082:  * makeAjaxRequest will form a GET request from the server.
083:  * Any arguments past the first 3 will be sent to the callback
084:  */
085: function makeAjaxRequest(callback, retXML, url) {
086:     args = [];
087:     for( i = 3; i < arguments.length; i++ ) {
088:         args.push( arguments[i] );
089:     }
090: 
091:     http_request = buildAjaxRequest(callback, retXML, false, args);
092: 
093:     /* This RANDOM query is so the page isn't cached */
094:     url += "&RANDOM=" + (Math.random() * Date.parse(new Date()))
095:     http_request.open('GET', url, true);
096:     http_request.send(null);
097: }
098: 
099: /*
100:  * makeAjaxPost will form a POST request to the server.
101:  * params should be a properly formatted line (id=3&submit=true)
102:  * Any arguments past the first 4 will be sent to the callback
103:  */
104: function makeAjaxPost(callback, retXML, url, params) {
105: 
106:     args = [];
107:     for( i = 4; i < arguments.length; i++ ) {
108:         args.push( arguments[i] );
109:     }
110: 
111:     http_request = buildAjaxRequest(callback, retXML, true, args);
112: 
113:     http_request.open("POST", url, true);
114:     http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
115:     http_request.send(params);
116: 
117:     return false;
118: }