inc: A super-tiny client-side include JavaScript jQuery plugin

Posted 2007-10-04 in JavaScript by Johann.

Is 2007 2015 going to be the year of the JavaScript client-side include? After Mark Nottingham’s HInclude and the equally cool purple-include, I thought it was time to do something new.

How about

  • declarative inclusion of remote content
  • pre- and post-inclusion callbacks to transform content
  • all this in less than 250 B after GZIP?

inc does this.

Demo

  • Replace this text with a mildly amusing text.
  • Include another text snippet and uppercase it.
  • This pseudo text will be replaced on load.
  • Test callback method.

Usage

Add the inc JavaScript after a recent jQuery build.

<script type="text/javascript" src="jquery-1.6.3.js"></script>
<script type="text/javascript">(function(a){a.fn.inc=function(b,c,d,e){return this.length&&b?this.each(function(){e=a(this);a.ajax({url:b,success:function(f,g,h){e.html(a.isFunction(c)?c(f,b):f);a.isFunction(d)&&d(b,g,h)}})}):this};a(function(){a('[class*="inc:"]').each(function(){var b=/inc:(\S+)/.exec(this.className||"");b&&a(this).inc(unescape(b[1]))})})})(jQuery);</script>

I recommend inlining the script since it’s so small. If you have your own scripts to concatenate and minify JavaScript files, you can download jQuery inc in its uncompressed form.

Automatic inclusion

If you would like to have some content automatically included once the page is loaded, use the inc CSS class and add the resource to load. The URI must be URL-encoded.

<p class="inc:footer.html">This text will be replaced
    with footer.html</p>

<p class="small inc:footer.html inactive">This text will
    be replaced with footer.html as well</p>

<p class="inc:la%20la%20la.html">This text will be replaced
    with “la la la.html”</p>

Programatic includes

You can use the jQuery.inc function to include resources. It takes three arguments, the URL to load, an optional function that is called with the content of the URL and an optional function that is called once the content is included.

<p onclick="$(this).inc('something.html');">Include something.</p>
$('#bla').inc('bla.html', function(a) { return a.toLowerCase() });

$('#bla').inc('bla.html', null, function() {
    alert('content is now live') });

License

MIT license.

History

I have been doing client-side includes for a while on my home page and later on the “page not found” page.

Changelog

  • 2015-02-13: Updated and checked with various jQuery versions between 1.2.6 and 2.1.3. Removed alternative code for IE because jQuery can use the native XMLHttpRequest object instead of ActiveX controls.
  • 2011-09-11: Update for newer jQuery versions plus various optimizations. Saving almost 100 B.
  • 2008-01-22: Pages would even be included if no elements were matching. Added post-inclusion callback.
  • 2007-12-19: Fixed two bugs in Internet Explorer.
  • 2007-12-11: jQuery 1.2 broke inc.
  • 2007-10-09: Made even smaller and used more jQuery functionality.
  • 2007-10-04: Initial release.

8 comments

#1 2007-10-19 by Terry McLaughlin

I have come across an issue in IE6 when using your script to include tables. IE6 displays the table, but does not the styles defined in a linked CSS file. Is this something you've come across before?

#2 2008-01-24 by Johann

Terry,

I am using this script only to include snippets, not full documents. I could imagine that the paths are different in the included document and the document it is included in.

#3 2008-02-01 by Alex

That's really cool! Especially as I'm already using jQuery in the site. May suggest an improvement: I'm using thickbox. tb_init sees for < a> tags onload. But obviously, CSI happens after. To make it work, need to set timers or hack with js, etc, so the elegant, tag-only idea won't work out of the box. Would be nice CSI to re-init jQuery, and by extent, thickbox, after it loads its stuff.
Keep up the smart work! :-)

#4 2008-02-01 by Johann

Alex,

thanks for the idea. Maybe I will add a callback function to inc that is called afer the content is set. For the time being, you can set a timer in the optional callback function.

#5 2008-09-06 by Mitch

Can the script include relative path URLs like ../somepath/tomy/file.html ?

#6 2008-09-28 by Johann

Mitch,

that should work just fine.

#7 2009-07-21 by Ron

I must have misunderstood "declarative inclusion of remote content" -- while I don't have need (at the moment) to include content from other domains, I would like to include, say, a footer chunk in a lower directory. For instance: <span class="inc:../footer.htm" id="footer_"></span>

How do you declare the remote content?

-ron

#8 2009-07-21 by Johann

Ron,

"remote content" doesn't mean content from another domain.

The code snippet you posted would work. In fact, it worked so well that I spent half an hour figuring out where the requests to footer.htm came from since you had not escaped your HTML and the empty span was invisible. :-(

Subscribe

RSS 2.0, Atom or subscribe by Email.

Top Posts

  1. DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
  2. 6 fast jQuery Tips: More basic Snippets
  3. xslt.js version 3.2 released
  4. xslt.js version 3.0 released XML XSLT now with jQuery plugin
  5. Forum Scanners - prevent forum abuse
  6. Automate JavaScript compression with YUI Compressor and /packer/

Navigation