JQuery for Everyone: Faster than Document.Ready
Sometimes I want scripts to work as soon as possible, possibly even before document.ready.
For style changes, one option is to add CSS link or style tags to the page. However, if the CSS is tightly married to a JavaScript solution, it’s better to keep the styling changes based in JavaScript.
So here’s my approach: I wrote a jQuery plugin that dynamically adds SCRIPT and LINK tags to the page. That allows me to dynamically load JavaScript or CSS. The added benefit of this method, I utilize the browser’s cache as much as possible (editing web.config works even better if you have access) and all of the files are visible through Firebug as if they were loaded normally. That doesn’t happen when you load files using AJAX.
So, for my example, I want all of the submenu sections of my left nav to start closed, but only if jQuery loads and my other script to open and close those sections will run on document.ready.
As a result of this technique, the navigation behavior completely degrades (to a normal SP behavior) if any part of this does not load. But if it does load, the appearance is seamless to the user–you can’t see the sections contracting as the page loads.
Click “Read more…” to see the new plugin code and the example:
<!-- load jQuery from local library or src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js'--> <script src="/javascripts/jquery.min.js" type="text/javascript"></script> <script type="text/javascript">/* * includeScript plugin * Copyright (c) 2009 Paul Grenier (endusersharepoint.com) * Licensed under the MIT (MIT-LICENSE.txt) */ (function($){ var _debug = function(msg,type){ if (window.console && window.console.log){ if (!type){type="log";} console[type](msg); return true; } return false; }; $.fn.includeFile = function(options, callback){ var defaults = {"tag":"script", "url":"", "media":"screen", "rel":"stylesheet" }, o = $.extend({},defaults,options), head = this[0], file = document.createElement(o.tag); switch (o.tag){ case "script": file.src = o.url; file.type = o.type?o.type:"text/javascript"; if (o.charset){file.charset=o.charset;} if (o.defer){file.defer="defer";} break; case "link": file.type = o.type?o.type:"text/css"; file.rel = "stylesheet"; file.href = o.url; file.media = "screen"; if (o.target){file.target=o.target;} if (o.rev){file.rev=o.rev;} if (o.hreflang){file.hreflang=o.hreflang;} if (o.charset){file.charset=o.charset;} if (callback){_debug("not all browsers support callback for <"+o.tag+">","warn");} break; default: _debug("unknown tag: <"+o.tag+">","error"); return $(this); } file.onload = file.onreadystatechange = function(){ if (!this.readyState || this.readyState == "loaded" || this.readyState == "complete"){ if (callback && typeof callback=="function"){callback(o);} file.onload = file.onreadystatechange = null; } }; head.appendChild(file); return $(this); }; })(jQuery); //end plugin //start instructions $("head:first") .includeFile({"url":"/javascripts/accordion-nav.js"},function(){ $("head:first").includeFile({"tag":"link","url":"/javascripts/leftnav.css"}); //load the css file as a callback }) .includeFile({"url":"/javascripts/expand-collapse-all.js"}) //another call chained from the first .includeFile({"url":"/javascripts/ows.js"}); //end with semi-colon //end instructions </script>
- JQuery for Everyone: Accordion Left Nav
- JQuery for Everyone: Print (Any) Web Part
- JQuery for Everyone: HTML Calculated Column
- JQuery for Everyone: Dressing-up Links Pt1
- JQuery for Everyone: Dressing-up Links Pt2
- JQuery for Everyone: Dressing-up Links Pt3
- JQuery for Everyone: Cleaning Windows Pt1
- JQuery for Everyone: Cleaning Windows Pt2
- JQuery for Everyone: Fixing the Gantt View
- JQuery for Everyone: Dynamically Sizing Excel Web Parts
- JQuery for Everyone: Manually Resizing Web Parts
- JQuery for Everyone: Total Calculated Columns
- JQuery for Everyone: Total of Time Differences
- JQuery for Everyone: Fixing Configured Web Part Height
- JQuery for Everyone: Expand/Collapse All Groups
- JQuery for Everyone: Preview Pane for Multiple Lists
- JQuery for Everyone: Preview Pane for Calendar View
- JQuery for Everyone: Degrading Dynamic Script Loader
- JQuery for Everyone: Force Checkout
- JQuery for Everyone: Replacing [Today]
- JQuery for Everyone: Whether They Want It Or Not
- JQuery for Everyone: Linking the Attachment Icon
- JQuery for Everyone: Aspect-Oriented Programming with jQuery
- JQuery for Everyone: AOP in Action - loadTip Gone Wild
- JQuery for Everyone: Wiki Outbound Links
- JQuery for Everyone: Collapse Text in List View
- JQuery for Everyone: AOP in Action - Clone List Header
- JQuery for Everyone: $.grep and calcHTML Revisited
- JQuery for Everyone: Evolution of the Preview
- JQuery for Everyone: Create a Client-Side Object Model
- JQuery for Everyone: Print (Any) Web Part(s) Plugin
- JQuery for Everyone: Minimal AOP and Elegant Modularity
- JQuery for Everyone: Cookies and Plugins
- JQuery for Everyone: Live Events vs. AOP
- JQuery for Everyone: Live Preview Pane
- JQuery for Everyone: Pre-populate Form Fields
- JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
- Use Firebug in IE
- JQuery for Everyone: Extending OWS API for Calculated Columns
- JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
- JQuery for Everyone: Email a List of People with OWS
- JQuery for Everyone: Faster than Document.Ready
- jQuery for Everyone: Collapse or Prepopulate Form Fields
- jQuery for Everyone: Hourly Summary Web Part
- jQuery for Everyone: "Read More..." On a Blog Site
- jQuery for Everyone: Slick Speed Test
- jQuery for Everyone: The SharePoint Game Changer
- JQuery For Everyone: Live LoadTip
“read more” does not work ?????
Zuber – Sure does… just checked. How were you able to comment if the read more link didn’t work :-) – Mark
There was a problem with the script, it got munched by Wordpress. Example should be visible now.
Interested in checking this out – but maybe its a monday morning thing but I cant find a “read more” link on the page.
Addtionally, where do you source the accordion-nav.js, leftnav.css, expand-collapse-all.js and ows.js files from?
@Aaron,
The link only shows when the page is viewed on the main page, as an individual article, the code is visible without clicking a link.
The various scripts are just that. The only one I want to run ASAP (so users don’t see the collapsing action) is accordion left nav. See a similar script here:
http://www.endusersharepoint.com/?p=1640
There seems to be an issue with this little jQuery plugin and Google’s Chrome browser. Here’s an example:
I have the following page:
Layout Example
Center
Go to the
Demos page
North
South
East
West
Simple.js contains the following:
$(”head:first”).includeFile({”url”:”js/ui/jquery.layout.js”});
$(document).ready(function () {
$(’body’).layout({ applyDefaultStyles: true });
});
When I use the scriptLoader.js (my name for the above code), Chrome does not apply the layout. It
seems to complain about ‘body’ not having a ‘layout’ method. IE and
FF work with out error. If I move the jquery.layout.js include to a
normal script tag in the html file (instead of having it be
dynamically included), it works fine in all 3 browsers.
Any idea what Chrome’s issue might be?
@Danno,
Document.ready only delays running the script until the document loads but it processes inline. When you called .layout, the layout.js file had not completely loaded.
You must run the call to .layout after layout.js has loaded, so we use a callback function, like this:
Since that anonymous function affects the DOM, we’ll still want to delay execution until the DOM loads, so you can wrap it in the document.ready (or equivalent):
I haven’t tested this, but I hope it helps.