jquery.ajaxContent.pack.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. *ajaxContent - jQuery plugin for accessible, unobtrusive and easy ajax behaviour.
  3. * @Version 2.0
  4. *
  5. * @requires jQuery v 1.0.1
  6. *
  7. * http://www.andreacfm.com/jquery-plugins
  8. *
  9. * Copyright (c) 2007 Andrea Campolonghi (andreacfm.com)
  10. * Dual licensed under the MIT and GPL licenses:
  11. * http://www.opensource.org/licenses/mit-license.php
  12. * http://www.gnu.org/licenses/gpl.html
  13. */
  14. (function($) {
  15. $.fn.ajaxContent = function(options) {
  16. var defaults = $.extend({}, $.fn.ajaxContent.defaults, options);
  17. if (defaults.debug == 'true') {
  18. debug(this)
  19. }
  20. ;
  21. return this.each(function() {
  22. var $obj = $(this);
  23. var o = $.meta ? $.extend({}, defaults, $obj.data()) : defaults;
  24. var url = $obj.attr('href');
  25. var $target = $(o.target);
  26. $obj.bind(o.event, function() {
  27. if (o.loader == 'true') {
  28. var loadingMessage;
  29. if (o.loaderType == 'img') {
  30. loadingMessage = '<img class="ajaxload" src=\"' + o.loadingMsg + '\"/>'
  31. }
  32. $target.html(loadingMessage)
  33. }
  34. $('a.' + o.currentClass).removeClass(o.currentClass);
  35. $obj.addClass(o.currentClass);
  36. $.ajax({type: o.type, url: url, success: function(msg) {
  37. $target.html(msg);
  38. if (typeof o.success == 'function') {
  39. o.success($obj, $target, msg)
  40. }
  41. }, error: function() {
  42. $target.html("<p>" + o.errorMsg + "</p>");
  43. if (typeof o.error == 'function') {
  44. o.error($target)
  45. }
  46. }});
  47. return false
  48. })
  49. })
  50. };
  51. function debug($obj) {
  52. if (window.console && window.console.log)
  53. window.console.log('selection count: ' + $obj.size() + ' with class:' + $obj.attr('class'))
  54. }}
  55. )(jQuery);
  56. $.fn.ajaxContent.defaults = {target: '#ajaxContent', type: 'get', event: 'click', loader: 'true', loaderType: 'text', loadingMsg: 'Loading...', errorMsg: ' ', currentClass: 'selected', success: '', error: '', debug: 'false'};