jQuery Cross-Site AJAX Plugin

April 12th, 2007 by Ralf S. Engelschall

It is well known that with the help of a dynamically generated XHTML <script> DOM element one can achieve a portable cross-site variation of an AJAX-style client-server communication as the <script> is not staying under the “same-origin security policy” which restricts the regular AJAX methods. This allows one to load code from a third-party URL in the background.

For this in JavaScript one generates a <script> DOM element with a type attribute set to text/javascript and a src attribute set to the URL of the third-partly URL. That’s it. Well, more or less. But as a jQuery user I wanted this functionality abstracted with a variation of the already existing (plain AJAX) jQuery.getScript() function, which in turn especially allows me to execute a callback function once the script is loaded. And for cleanup and memory leak prevention reasons I wanted that the dynamically generated <script> DOM node is garbage collected automatically, too.

My result is a small jQuery plugin jquery.xsajax.js which provides just this. Find this jQuery plugin in the File Repository area.

Leave a Reply