Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. /*
  2. * anchor-include pattern for already-functional links that work as a client-side include
  3. * Copyright 2011, Scott Jehl, scottjehl.com
  4. * Dual licensed under the MIT
  5. * Idea from Scott Gonzalez
  6. * to use, place attributes on an already-functional anchor pointing to content
  7. * that should either replace, or insert before or after that anchor
  8. * after the page has loaded
  9. * Replace: <a href="..." data-replace="articles/latest/fragment">Latest Articles</a>
  10. * Before: <a href="..." data-before="articles/latest/fragment">Latest Articles</a>
  11. * After: <a href="..." data-after="articles/latest/fragment">Latest Articles</a>
  12. * Also, the data-threshold attr allows a min width for this to apply.
  13. * On domready, you can use it like this:
  14. $("[data-append],[data-replace],[data-after],[data-before]").ajaxInclude();
  15. */
  16. (function( $ ){
  17. $.fn.ajaxInclude = function( e ) {
  18. return this.each(function() {
  19. var el = $( this ),
  20. target = el.data( "target" ),
  21. targetEl = target && $( target ) || el,
  22. threshold = screen.width > parseInt( el.data( "threshold" ) || 0 ),
  23. methods = [ "append", "replace", "before", "after" ],
  24. method,
  25. url;
  26.  
  27. if ( threshold ) {
  28.  
  29. for( var ml = methods.length, i=0; i < ml; i++ ){
  30. if( el.is( "[data-" + methods[ i ] + "]" ) ){
  31. method = methods[ i ];
  32. url = el.data( method );
  33. }
  34. }
  35.  
  36. if( method === "replace" ){
  37. method += "With";
  38. }
  39.  
  40. if( url && method ){
  41. $.get( url, function( data ) {
  42. var responseEl = $(data),
  43. eTarget = method === "replaceWith" ? responseEl : el;
  44.  
  45. targetEl[ method ]( responseEl );
  46.  
  47. eTarget.trigger( "ajaxInclude", [eTarget, data] );
  48. });
  49. }
  50.  
  51. }
  52. });
  53. };
  54. })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement