Load jQuery Plugins Only When Required (Runtime)

July 16, 2009 · 2 comments   496 views

in Technical Publications

I have often observed that websites that are template-driven (like blogs, cms, etc), tend to load all the required scripts and css in every page. One such example would be WordPress blogs with plugins for various types of content processing (overlay, tooltip, animation, etc.) Every page of such a WordPress driven site loads the relevant plugin stylesheets and javascripts, even though it might not be required for that particular page.

Here is a jQuery code I wrote in order to load prettyPhoto jQuery plugin (prettyPhoto homepage) only when required. This process can also be applied to other plugins by analyzing the plugin triggers from within the rest of the page content.

  1. if(jQuery) {
  2.    jQuery(document).ready( function() {
  3.  
  4.       // check if rel=prettyphoto exists (plugin trigger)
  5.       if ( jQuery("a[rel^='prettyPhoto']").length > 0 ) {
  6.  
  7.          // prettyPhoto package location
  8.          pfBaseLoc = ‘/wp-content/assets/prettyPhoto_compressed_2.4.3/’;
  9.  
  10.          // load pretty photo css
  11.          jQuery(‘<link>’).attr({
  12.             ‘rel’: ‘stylesheet’,
  13.             ‘type’: ‘text/css’,
  14.             ‘href’: pfBaseLoc + ‘css/prettyPhoto.css’,
  15.             ‘media’: ‘screen’ }).appendTo(document.getElementsByTagName(‘head’)[0]);
  16.  
  17.          // load prettyphoto js (getscript of jQuery prevents caching so using ajax)
  18.          jQuery.ajax({
  19.             type: ‘GET’,
  20.             url: pfBaseLoc + ‘js/jquery.prettyPhoto.js’,
  21.             cache: true,
  22.             success: function() { jQuery("a[rel^='prettyPhoto']").prettyPhoto(); },
  23.             dataType: ‘script’,
  24.             data: null
  25.          });
  26.       }
  27.    });
  28.  
  29. }

Note that in the above code, I did not use the handy getScript() of jQuery. This is because the getScript() function prevents caching of the JS file. That in turn may cause more server requests than needed. So, alternatively use the ajax() method to fetch script.

Web developers should actively adopt this aproach so that unneeded scripts are not loaded and consequently, page load time is reduced.

  1.        

Related posts:

  1. Analytics jQuery Plugin 2.0 Supports Full Tracking API
  2. Transpose An Array In JavaScript and jQuery
  3. jQuery Plugin for FusionCharts 1.0 Beta Released
  4. Executing jQuery Within FusionCharts Link Attribute
  5. Google Analytics jQuery Plugin

{ 2 comments… read them below or add one }

1 daryll July 17, 2009 at 18:24

The code does not work upon copy-paste. The quotation marks are all screwed up. You should use Google Prettifier to do syntax highlighting. Whatever you are using now isn’ that great.

Reply

2 Shamasis Bhattacharya July 17, 2009 at 18:30

Damn yes! i know it. But I am using Syntax highlighter from Geshi! It is server-side and has a seamless wordpress plugin. I will soon fix it. :)

Reply

Leave a Comment