tot - jquery/jquery.xsajax.js 26
Not logged in
[Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Timeline
  [Raw
jquery/jquery.xsajax.js 26
/*
**  jquery.xsajax.js -- jQuery plugin for Cross-Site AJAX
**  Copyright (c) 2007 Ralf S. Engelschall <rse@engelschall.com>
**  Licensed under GPL <http://www.gnu.org/licenses/gpl.txt>
**
**  $LastChangedDate$
**  $LastChangedRevision$
*/

(function($){
    $.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 on-load handler for
               garbage collecting <script> node after loading
               or calling a custom callback function */
            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) {
                    /* Apple Safari requires polling */
                    $.safariTimer = setInterval(function () {
                        if (   node.get(0).readyState == "complete"
                            || node.get(0).readyState == "loaded"  ) {
                            clearInterval($.safariTimer);
                            $.safariTimer = null;
                            callback.call(node.get(0));
                        }
                    }, 100);
                }
                else {
                    /* use regular "onload" event for all other browsers */
                    $(node).load(callback);
                }
            }

            /* inject <script> node into <head> of document */
            $('head', document).append(node);
        }
    });
})(jQuery);