3 // http://jquery.com/docs/ajax/
5 if ( typeof XMLHttpRequest == 'undefined' && typeof window.ActiveXObject == 'function') {
6 XMLHttpRequest = function() {
7 return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ?
8 "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP");
12 // Counter for holding the active query's
15 $.xml = function( type, url, data, ret ) {
17 ret = type.onComplete;
18 var onSuccess = type.onSuccess;
19 var onError = type.onError;
25 var xml = new XMLHttpRequest();
29 xml.open(type || "GET", url, true);
31 xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
33 // Set header so calling script knows that it's an XMLHttpRequest
34 xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
36 /* Borrowed from Prototype:
37 * Force "Connection: close" for Mozilla browsers to work around
38 * a bug where XMLHttpReqeuest sends an incorrect Content-length
39 * header. See Mozilla Bugzilla #246651.
41 if ( xml.overrideMimeType )
42 xml.setRequestHeader('Connection', 'close');
44 xml.onreadystatechange = function() {
46 if ( xml.readyState == 1 ) {
50 // Show loader if needed
51 if ( ($.xmlActive >= 1) && ($.xmlCreate) )
55 // Socket is closed and data is available
56 if ( xml.readyState == 4 ) {
60 // Hide loader if needed
61 if ( ($.xmlActive <= 0) && ($.xmlDestroy) ) {
66 if ( ( xml.status && ( xml.status >= 200 && xml.status < 300 ) || xml.status == 304 ) ||
67 !xml.status && location.protocol == 'file:' ) {
70 } else if ( onError ) {
84 $.httpData = function(r,type) {
85 return r.getResponseHeader("content-type").indexOf("xml") > 0 || type == "xml" ?
86 r.responseXML : r.responseText;
89 $.get = function( url, ret, type ) {
90 $.xml( "GET", url, null, function(r) {
91 if ( ret ) { ret( $.httpData(r,type) ); }
95 $.getXML = function( url, ret ) {
96 $.get( url, ret, "xml" );
99 $.post = function( url, data, ret, type ) {
100 $.xml( "POST", url, $.param(data), function(r) {
101 if ( ret ) { ret( $.httpData(r,type) ); }
105 $.postXML = function( url, data, ret ) {
106 $.post( url, data, ret, "xml" );
109 $.param = function(a) {
111 if (a && typeof a == 'object' && a.constructor == Array) {
112 for ( var i=0; i < a.length; i++ ) {
113 s[s.length] = a[i].name + "=" + encodeURIComponent( a[i].value );
117 s[s.length] = j + "=" + encodeURIComponent( a[j] );
123 $.fn.load = function(a,o,f) {
125 // I overwrote the event plugin's .load
126 // this won't happen again, I hope -John
127 if ( a && a.constructor == Function ) {
128 return this.bind("load", a);
132 if ( o && o.constructor == Function ) {
136 if (typeof o !== 'undefined') {
141 $.xml(t,a,o,function(res){
142 // Assign it and execute all scripts
143 self.html(res.responseText).find("script").each(function(){
144 try { eval( this.text || this.textContent || this.innerHTML || ""); } catch(e){}
148 if (f && f.constructor == Function)
155 * Initial frontend function to submit form variables. This function
156 * is for registering coordinates, in the case of an image being used
157 * as the submit element, and sets up an event to listen and wait for
158 * a form submit click. It then calls any following chained functions
159 * to actually gather the variables and submit them.
161 * Usage examples, when used with getForm().putForm():
163 * 1. Just eval the results returned from the backend.
164 * $('#form-id').form();
166 * 2. Render backend results directly to target ID (expects (x)HTML).
167 * $('#form-id').form('#target-id');
169 * 3. Submit to backend URL (form action) then call this function.
170 * $('#form-id').form(post_callback);
172 * 4. Load target ID with backend results then call a function.
173 * $('#form-id').form('#target-id', null, post_callback);
175 * 5. Call a browser function (for validation) and then (optionally)
176 * load server results to target ID.
177 * $('#form-id').form('#target-id', pre_callback);
179 * 6. Call validation function first then load server results to
180 * target ID and then also call a browser function.
181 * $('#form-id').form('#target-id', pre_callback, post_callback);
183 * @param target arg for the target id element to render
184 * @param pre_cb callback function before submission
185 * @param post_cb callback after any results are returned
186 * @return "this" object
187 * @see getForm(), putForm()
188 * @author Mark Constable (markc@renta.net)
189 * @author G. vd Hoven, Mike Alsup, Sam Collett
192 $.fn.form = function(target, pre_cb, post_cb) {
193 $('input[@type="submit"],input[@type="image"]', this).click(function(ev){
194 this.form.clicked = this;
195 if (ev.offsetX != undefined) {
196 this.form.clicked_x = ev.offsetX;
197 this.form.clicked_y = ev.offsetY;
199 this.form.clicked_x = ev.pageX - this.offsetLeft;
200 this.form.clicked_y = ev.pageY - this.offsetTop;
203 this.submit(function(e){
205 $(this).getForm().putForm(target, pre_cb, post_cb);
211 * This function gathers form element variables into an array that
212 * is embedded into the current "this" variable as "this.vars". It
213 * is normally used in conjunction with form() and putForm() but can
214 * be used standalone as long as an image is not used for submission.
216 * Standalone usage examples:
218 * 1. Gather form vars and return array to LHS variable.
219 * var myform = $('#form-id').getForm();
221 * 2. Provide a serialized URL-ready string (after 1. above).
222 * var mystring = $.param(myform.vars);
224 * 3. Gather form vars and send to RHS plugin via "this.vars".
225 * $('#form-id').getForm().some_other_plugin();
227 * @return "this" object
228 * @see form(), putForm()
229 * @author Mark Constable (markc@renta.net)
230 * @author G. vd Hoven, Mike Alsup, Sam Collett
233 $.fn.getForm = function() {
235 var ok = {INPUT:true, TEXTAREA:true, OPTION:true};
236 $('*', this).each(function() {
237 if (this.disabled || this.type == 'reset' || (this.type == 'checkbox' && !this.checked) || (this.type == 'radio' && !this.checked))
240 if (this.type == 'submit' || this.type == 'image') {
241 if (this.form.clicked != this)
244 if (this.type == 'image') {
245 if (this.form.clicked_x) {
246 a.push({name: this.name+'_x', value: this.form.clicked_x});
247 a.push({name: this.name+'_y', value: this.form.clicked_y});
253 if (!ok[this.nodeName.toUpperCase()])
256 var par = this.parentNode;
257 var p = par.nodeName.toUpperCase();
258 if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected)
261 var n = this.name || par.name;
262 if (!n && p == 'OPTGROUP' && (par = par.parentNode))
268 a.push({name: n, value: this.value});
277 * Final form submission plugin usually used in conjunction with
278 * form() and getForm(). If a second argument is a valid function
279 * then it will be called before the form vars are sent to the
280 * backend. If this pre-submit function returns exactly "false"
281 * then it will abort further processing otherwise the process
282 * will continue according to the first and third arguments.
284 * If the first argument is a function, and it exists, then the form
285 * values will be submitted and that callback function called. If
286 * the first argument is a string value then the "load()" plugin
287 * will be called which will populate the innerHTML of the indicated
288 * element and a callback will be called if there is third argument.
289 * If there are no arguments then the form values are submitted with
290 * an additional variable (evaljs=1) which indicates to the backend
291 * to to prepare the returned results for evaluation, ie; the result
292 * needs to be valid javascript all on a single line.
296 * $.fn.myvars = function() {
298 * for (var i in this) {
299 * if (this[i] instanceof Function || this[i] == null) continue;
300 * this.vars.push({name: i, value: this[i].length});
305 * precb = function(vars) {
306 * return confirm('Submit these values?\n\n'+$.param(vars));
309 * $('*').myvars().putForm('#mytarget',precb,null,'myhandler.php');
311 * @param target arg for the target id element to render
312 * @param pre_cb callback function before submission
313 * @param post_cb callback after any results are returned
314 * @param url form action override
315 * @param mth form method override
316 * @return "this" object
317 * @see form(), getForm(), load(), xml()
318 * @author Mark Constable (markc@renta.net)
319 * @author G. vd Hoven, Mike Alsup, Sam Collett
322 $.fn.putForm = function(target, pre_cb, post_cb, url, mth) {
323 if (pre_cb && pre_cb.constructor == Function)
324 if (pre_cb(this.vars) === false)
328 var url = url || f.action || '';
329 var mth = mth || f.method || 'POST';
331 if (target && target.constructor == Function) {
332 $.xml(mth, url, $.param(this.vars), target);
333 } else if (target && target.constructor == String) {
334 $(target).load(url, this.vars, post_cb);
336 this.vars.push({name: 'evaljs', value: 1});
337 $.xml(mth, url, $.param(this.vars), function(r) { eval(r.responseText); });