jquery xsajax js -- jQuery plugin for Cross-Site AJAX-style Javascript

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
** jquery.xsajax.js -- jQuery plugin for Cross-Site AJAX-style Javascript loading
** Copyright (c) 2007 Ralf S. Engelschall <rse@engelschall.com>
** Licensed under GPL <http://www.gnu.org/licenses/gpl.txt>
**
** $LastChangedDate: 2007-04-13 12:45:27 +0300 (Fri, 13 Apr 2007) $
** $LastChangedRevision: 35 $
*/
(function($){
if ( $.browser.safari
|| navigator.userAgent.match(/Konqueror/i)) {
$.extend({
_xsajax$node: [],
_xsajax$nodes: 0
});
}
$.extend({
getScriptXS: function () {
/* determine arguments */
var arg = {
'url': null,
'gc': true,
'cb': null,
'cb_args': null
};
if (typeof arguments[0] == "string") {
/* simple usage */
arg.url = arguments[0];
if (typeof arguments[1] == "function")
arg.cb = arguments[1];
}
else if (typeof arguments[0] == "object") {
/* flexible usage */
for (var option in arguments[0])
if (typeof arg[option] != "undefined")
arg[option] = arguments[0][option];
}
/* generate <script> node */
var node =
$(document.createElement('script'))
.attr('type', 'text/javascript')
.attr('src', arg.url);
/* optionally apply event handler to <script> node for
garbage collecting <script> node after loading and/or
calling a custom callback function */
var node_helper = null;
if (arg.gc || arg.cb !== null) {
var callback = function () {
if (arg.cb !== null) {
var args = arg.cb_args;
if (args === null)
args = [];
else if (!( typeof args === "object"
&& args instanceof Array ))
args = [ args ];
arg.cb.apply(this, args);
}
if (arg.gc)
$(this).remove();
};
if ($.browser.msie) {
/* MSIE doesn't support the "onload" event on
<script> nodes, but it at least supports an
"onreadystatechange" event instead. But notice:
according to the MSDN documentation we would have
to look for the state "complete", but in practice
for <script> the state transitions from "loading"
to "loaded". So, we check for both here... */
node.get(0).onreadystatechange = function () {
if ( this.readyState == "complete"
|| this.readyState == "loaded" )
callback.call(this);
};
}
else if ( $.browser.safari
|| navigator.userAgent.match(/Konqueror/i)) {
/* Safari/WebKit and Konqueror/KHTML do not emit
_any_ events at all, but we can exploit the fact
that dynamically generated <script> DOM nodes
are executed in sequence (although the scripts
theirself are still loaded in parallel) */
$._xsajax$nodes++;
var helper =
'var ctx = jQuery._xsajax$node[' + $._xsajax$nodes + '];' +
'ctx.callback.call(ctx.node);' +
'setTimeout(function () {' +
' jQuery(ctx.node_helper).remove();' +
'}, 100);';
node_helper =
$(document.createElement('script'))
.attr('type', 'text/javascript')
.text(helper);
$._xsajax$node[$._xsajax$nodes] = {
callback: callback,
node: node.get(0),
node_helper: node_helper.get(0)
};
}
else {
/* Firefox, Opera and other reasonable browsers can
use the regular "onload" event... */
$(node).load(callback);
}
}
/* inject <script> node into <head> of document */
$('head', document).append(node);
/* optionally inject helper <script> node into <head>
(Notice: we have to use a strange indirection via
setTimeout() to insert this second <script> node here or
at least Konqueror (and perhaps also Safari) for unknown
reasons will not execute the first <script> node at all) */
if (node_helper !== null) {
setTimeout(function () {
$('head', document).append(node_helper)
}, 100);
}
}
});
})(jQuery);