Skip to content

Instantly share code, notes, and snippets.

@jchris
Created November 10, 2008 23:20
Show Gist options
  • Save jchris/23684 to your computer and use it in GitHub Desktop.
Save jchris/23684 to your computer and use it in GitHub Desktop.
for the cross domains
// cargo culted bits from here: http://dev.jquery.com/ticket/3342
// Usage:
// $.xdom.post('http://example.com/post-to-here',{"foo":"bar"});
// Sends a POST to http://example.com/post-to-here with content foo=bar
(function($) {
$.xdom = $.xdom || {};
function now(){
return +new Date;
}
var ifc = now();
$.fn.extend($.xdom,{
post : function(url, data) {
var ifr = "ifr" + ifc++;
var iid = 'xdom_' + ifr;
$('body').append('<iframe id="'+iid+'" name="'+iid+'"/>');
var iframe = $('#'+iid)[0];
var op8 = $.browser.opera && window.opera.version() < 9;
if ($.browser.msie || op8) iframe.src = 'javascript:false;document.write("");';
$(iframe).css({ position: 'absolute', top: '-1000px', left: '-1000px' });
var fid = 'form_' + ifr;
$('body').append('<form id="'+fid+'" name="'+fid
+ '" method="POST" action="'+url+'" target="'+iid+'"/>');
var form = $('#'+fid)[0];
$(form).css({position: 'absolute', top: '-1000px', left: '-1000px' });
$.each(data, function(key, value) {
var inid = 'input_'+ifr+'_'+key;
$(form).append('<input id="' + inid + '" name="'
+ key +'" type="text"/>');
$('#'+inid).val(value);
});
form.submit();
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment