It is well known that with the help of a dynamically generated
DOM node corresponding to a XHTML <script>
element, one can achieve a portable cross-site (XS) variation
of an AJAX-style client-server communication as the
<script> element is not staying under the
"same-origin security policy" which restricts all the regular
AJAX methods. This allows one to asynchronously load JavaScript
code from a third-party URL.
The JavaScript code in jquery.xsajax.js is a jQuery plugin, providing an
abstraction layer for this functionality by resembling the
standard AJAX-based jQuery.getScript(url, callback)
function with a companion jQuery.getScriptXS(url,
callback) function.
Hints
Notice 1: the point of this plugin is not the bare dynamic
generation of the <script>. The point is
that a callback function is executed once the script was
loaded and executed by the browser and that the generated
<script> is automatically garbage collected from
the DOM again afterwards.
Notice 2: the possibility to load a JavaScript file from a
sibling but third-party URL is also of great interest when
one wishes to set cookies for the third-party URLs. Suppose
you have three websites www.example.com, www.example.net and
www.example.org and on www.example.com/login you have your
customer login form. Once the customer was authenticated,
a cookie containing a "certificate" should be set. Setting
this for ".example.com" is trivial, but how do you at the
same time set it for all three domains?
The solution is this: www.example.com/login uses
jQuery.getScriptXS() to send the issued "certificate"
via a query string to both www.example.net/reflector and
www.example.org/reflector. Behind those URLs a small CGI
just converts the "certificate" in the query string into a
corresponding HTTP response cookie (now for its own domain!)
and returns even a possibly empty JavaScript script. Once
www.example.com/login has received the two final notification
(via the callback function) that both scripts were loaded, it
knows that the two third-party cookies were set and can proceed
by forwarding to the next page in sequence. Last hint: keep P3P
in mind for MSIE when setting the cookie in the HTTP response
;-)