Skip to content

Instantly share code, notes, and snippets.

@nicalpi
Created February 5, 2009 11:10
Show Gist options
  • Save nicalpi/58660 to your computer and use it in GitHub Desktop.
Save nicalpi/58660 to your computer and use it in GitHub Desktop.
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})
//Send data via get if JS enabled
jQuery.fn.getWithAjax = function() {
this.click(function() {
$.get($(this).attr("href"), $(this).serialize(), null, "script");
return false;
})
return this;
};
//-------------------------------------
//Send data via Post if JS enabled
jQuery.fn.postWithAjax = function() {
this.click(function() {
$.post($(this).attr("href"), $(this).serialize(), null, "script");
return false;
})
return this;
};
//-------------------------------------
//Sumit the forms with ajax if JS enables
jQuery.fn.submitWithAjax = function() {
this.submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
return false;
})
return this;
};
//----------------------------------------
$(document).ready(function() {
// All A tags with class 'get', 'post', 'put' or 'delete' and FORM tags with class 'ajax' will perform an ajax call
$('a.get').getWithAjax();
$('a.post').postWithAjax();
$('a.put').postWithAjax();
$('a.delete').postWithAjax();
$('form.ajax').submitWithAjax();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment