Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Anchor-include Pattern
/*
* anchor-include pattern for already-functional links that work as a client-side include
* Copyright 2011, Scott Jehl, scottjehl.com
* Dual licensed under the MIT
* Idea from Scott Gonzalez
* to use, place attributes on an already-functional anchor pointing to content
* that should either replace, or insert before or after that anchor
* after the page has loaded
* Replace: <a href="..." data-replace="articles/latest/fragment">Latest Articles</a>
* Before: <a href="..." data-before="articles/latest/fragment">Latest Articles</a>
* After: <a href="..." data-after="articles/latest/fragment">Latest Articles</a>
* On domready, you can use it like this:
$("[data-append],[data-replace],[data-after],[data-before]").ajaxInclude();
*/
(function( $ ){
if ( $.fn.jquery.replace(/\./g,'') < 143 )
{
throw new Error('jQuery 1.4.3 or higher is required');
return;
}
$.fn.ajaxInclude = function()
{
return this.each(function(){
var $el = $( this ),
target = $el.data( 'target' ),
$targetEl = target && $( target ) || $el,
methods = [ 'append', 'replace', 'before', 'after' ],
ml = methods.length,
method,
url;
while ( ml-- )
{
method = methods[ ml ];
if ( $el.is( '[data-' + method + ']' ) )
{
url = $el.data( method );
break;
}
}
if ( method == 'replace' )
{
method += 'With';
}
if ( url && method )
{
$.get( url, function( data ){
$el.trigger( 'ajaxInclude', [$targetEl, data] );
$targetEl[ method ]( data );
});
}
});
};
})( jQuery );
(function(a){if(a.fn.jquery.replace(/\./g,"")<143){throw new Error("jQuery 1.4.3 or higher is required");return}a.fn.ajaxInclude=function(){return this.each(function(){var d=a(this),f=d.data("target"),e=f&&a(f)||d,b=["append","replace","before","after"],h=b.length,g,c;while(h--){g=b[h];if(d.is("[data-"+g+"]")){c=d.data(g);break}}if(g=="replace"){g+="With"}if(c&&g){a.get(c,function(i){d.trigger("ajaxInclude",[e,i]);e[g](i)})}})}})(jQuery);
Owner

aarongustafson commented Oct 3, 2011

  • Improved compression potential by switching to a singular variable declaration block and decrementing while statement.
  • Added a check for jQuery 1.4.3 or higher (as native support for HTML5 data attributes in data() was introduced in that version)
  • switched to single quotes for strings (personal preference)
  • switched jQuery instance variables to be prefixed with "$" (for easier scanning)
Owner

aarongustafson commented Oct 3, 2011

Switched back to spaces (instead of tabs) and removed my console.log() stuff

maniqui commented Oct 17, 2011

Hi Aaron.

Quoted from your blog post From "Mobile Friendly" to "Mobile First":

Comments are now loaded via Ajax. I know, it sounds grazy, but it makes sense. By default, we include a link to an alternate version of the blog post template with comments exposed (well, really it’s the same template with an additional URL segment passed in). Then, using JavaScript, we look for that link and replace it with the comments thread after the page finishes loading. You know, progressive enhancement. The overall effect is that it reduces the time it takes to download the page, which means you get to the content you want to read faster. You can check out the code that makes it work over on Github (it’s a slightly modified version of Scott Jehl’s original script).

If I understand it correctly, and by looking at this gist, I assume that the link to the alternate version (the page that shows the blog post and the comments) looks something like this:

<a href="/articles/an-article-about-some-topic/?comments=1" data-replace="/articles/an-article-about-some-topic/comments/fragment/">Comments on this article</a>

(or similar)
Correct?

It's a nice idea, and it could certainly be extended to other secondary/auxiliary fragments, helping to strip down the page just to its main content (and maybe its main nav). This could bring some benefits not just for a mobile-first approach (smaller page size, content-centric page, faster rendering) but also in SEO (although I'd point the rel=canonical URL to the one also holding the comments).
Will take the chance to experiment with this approach on next project...

Owner

aarongustafson commented Oct 18, 2011

You got it. Here’s the actual line in the HTML of that article:

<a id="comment-loader" data-replace="/comments/from-mobile-friendly-to-mobile-first/" href="/archives/2011/10/12/from-mobile-friendly-to-mobile-first/comments/#comments">View comments on this entry and add your own</a>

Good call on the canonical URL. That hadn’t occurred to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment