Synopsis
1. Adopt a consistent approach when writing document ready events.
2. Use a document ready event in place of window onload.
3. No JavaScript or behavioural markup is to be included in HTML files.
4. Events must be late-bound within the $(document).ready()handler code itself.
5. Use the shorthand event syntax.
6. Anonymous function handlers must be limited to one line of code.
Document Ready Declaration
1. The handler for the document ready event must be an anonymous declared in the following form:
$(document).ready(function(){ //all initialization });
This is clear, readable, and should be used ahead of other possible invocations of this functionality, which include:
$(handler);
$().ready(handler); //not recommended
$(document).bind("ready", handler); //not fired if bound after document ready fires
(And, of course, ‘$’ is interchangeable with ‘jQuery’)
This standard stipulates the use of the given format, however the important aspect of this is the recommendation to adopt a consistent approach to writing document ready events.
2. A Document Ready handler must be specified in place of the window.onload event, except in the rare event where this is too soon: http://blog.arc90.com/2008/05/16/a-jquery-tip-dont-use-jquery/
Note: There is no limit on the number of document ready handlers that can be associated with a page. Event handlers are added to a stack that will be executed when the document is ready.
Binding and Behavioural Markup
3. No JavaScript or behavioural markup is to be included in HTML files. For example, the red-highlighted code should not be present in HTML any more:
<input type="button" id="MyButton" onclick="OnClickMyButton()">
4. Events must be late-bound within the $(document).ready()handler code itself. For example:
$(document).ready(function(){ $("#MyButton").click(MyFunc); }); function MyFunc(event){ }
5. Use the shorthand event syntax:
$("#MyButton").click(MyFunc); //use this
$("#MyButton").bind("click", MyFunc); //instead of this
Event Handlers and Anonymous Functions
6. jQuery allows developers to specify anonymous functions for event handlers. For example:
$("#MyButton").click(function(){//code});
However, to avoid On Ready Bloating (a side effect of late-binding where the document.ready() function handler becomes large, cumbersome, and unmaintainable), these anonymous function handlers must be limited to one line of code:
$("#MyButton").click(function(){ MyFunction(); });
If we need to refer to the this object, we wrap it in a jQuery object, and pass it within our anonymous function (it is better practice to wrap in a jQuery object early on):
$("#MyButton").click(function(){ MyFunction($(this)); });
jQuery will automatically pass the event object when calling our anonymous function. We can access it as follows:
$("#MyButton").click(function(event){ MyFunction($(this), event); });
Incidentally, the declaration for MyFunction for the above example would be:
function MyFunction($this, event){ //code }
As well as avoiding OnReady Bloating, this approach makes our code easier to debug and more readable.
jQuery Coding Standards Menu
[...] here: jQuery Coding Standards (3) – Events « James Wiseman If you enjoyed this article please consider sharing [...]
[...] Events [...]
beware that the $(document).ready event is fired before the page is fully loaded, and thus can have a bad influence on the time needed to load the page. Usually the $(window).ready event is a better choise.
[...] Events [...]
When you have this:
$(“#MyButton”).click(function(event){
MyFunction($(this), event);
});
…
function MyFunction($this, event){
//code
}
then you can just do:
$(“#MyButton”).click(MyFunction);
function MyFunction(event){
// $(this).something();
}
[...] Events [...]