jquery tips and tricks | performance | namespacing
Jquery performance tip
Although jQuery fails nicely if it does not find any matching elements, it still takes time to look for them. If you have one global JavaScript for your entire site, it may be tempting to throw every one of your jQuery functions into $(document).ready(function(){ // all my glorious code }). Don’t you dare. Only run functions that are applicable to the page. The most efficient way to do this is to use inline initialization functions so your template has full control over when and where JavaScript executes. For example, in your “article” page template, you would include the following code before the body close:
<script type="text/javascript> mylib.article.init(); </script> </body>
If your page template includes any variety of modules that may or may not be on the page, or for visual reasons you need them to initialize sooner, you could place the initialization function immediately after the module.
<ul id="traffic_light"> <li><input type="radio" name="light" value="red" /> Red</li> <li><input type="radio" name="light" value="yellow" /> Yellow</li> <li><input type="radio" name="light" value="green" /> Green</li> </ul> <script type="text/javascript> mylib.traffic_light.init(); </script>
Your Global JS library would look something like this:
var mylib =
{
article_page :
{
init : function()
{
// Article page specific jQuery functions.
}
},
traffic_light :
{
init : function()
{
// Traffic light specific jQuery functions.
}
}
}
Referer: http://www.artzstudio.com/2009/04/jquery-performance-rules/
Latest posts
- bootstrap popover | bootstrap popover hide on click | bootstrap popover hide on document.click
- Avoid console errors in browsers that lack a console | HTML5 boilerplate
- getting started with Sublime editor | how to install zen coding | zen coding for sublime editor
- getting started with backbone | Models in Backbone
- How to delete a model in backbone






