Skip to content

Instantly share code, notes, and snippets.

@rainchen
Created November 17, 2009 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rainchen/236757 to your computer and use it in GitHub Desktop.
Save rainchen/236757 to your computer and use it in GitHub Desktop.
// jquery ajax helper by RainChen @ 2009-11-4
// more detail: http://hi.baidu.com/rainchen/blog/item/8877861821904abe4bedbc07.html
// show the ajax result
app.ajax.showResult = function(options){
options = $.extend({title: '', body: '', width: 350, height: 200, iframe: false, zIndex: 7000}, options);
if(!$("#app_ajax_result").get(0)){
$(document.body).append('<div id="app_ajax_result"></div>');
$("#app_ajax_result").dialog({
title: '',
bgiframe: true,
width: options.width,
height: options.height,
modal: true
});
}
if(options.iframe){
$("#app_ajax_result").html("");
// show message in an iframe to make sure the css will not break the current page
var iframe = $('<iframe src="about:blank" border="0" frameborder="0" width="100%" height="100%"></iframe>').appendTo("#app_ajax_result");
iframe.load(function(){
$(this).contents().find('body').html(options.body);
show();
});
if($.browser.safari) iframe.load(); // When Safari loads an IFrame, it won’t fire its onload event if it or any of its parents has display:none.
}else{
$("#app_ajax_result").html(options.body);
show();
}
function show(){
$("#app_ajax_result").dialog('option', 'title', options.title);
$("#app_ajax_result").dialog('open');
}
};
// app.ajax.showResult({title:'hi', body: 'body', iframe:true});
// default error handler for ajax request
app.ajax.error = function(XMLHttpRequest, textStatus, errorThrown){
return app.ajax.showResult({title: XMLHttpRequest.statusText, body: XMLHttpRequest.responseText, width: 600, height: 400, iframe: true});
};
// setup AjaxHelpers
app.ajax.setup = function(){
// set authenticity koen for Rails
if(typeof AUTHENTICITY_TOKEN != 'undefined' && AUTHENTICITY_TOKEN != '')
{
$.ajaxSetup( {data: {authenticity_token: AUTHENTICITY_TOKEN}});
}
$.ajaxSetup({error: app.ajax.error});
};
// init when page loaded
$(function(){app.ajax.setup();});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment