Created
April 3, 2016 19:30
-
-
Save nicocrm/5238f9a4d13384a039db4cf4ff54cfd6 to your computer and use it in GitHub Desktop.
Ajaxify links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
// Ajaxify links. | |
// $('a.xxx').ajaxLink('.where-to-load-new-content', | |
// '.where-to-display-loading-splash', | |
// 'html to display in loadingContainer (will be appended)', | |
// function to invoke when loading is complete) | |
$.fn.ajaxLink = function(containerSelector, loadingContainer, loadingHtml, callback) { | |
var $this = this; | |
$this.click(function() { | |
var href = $(this).attr('href'); | |
$(loadingContainer).append(loadingHtml); | |
$.ajax(href, { | |
dataType: 'html', | |
success: function(data) { | |
var loaded = $(data).find(containerSelector); | |
$(containerSelector).html(loaded.html()); | |
callback(); | |
} | |
}); | |
return false; | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment