I just have a question about writing functions in jQuery. When defining your own functions should they be written inside $(function(){}); or outside it? Please note that these are just example functions and could be anything. I chose a jQuery function and a native JavaScript to see if there are any differences i.e. should a self-defined jQuery function be defined inside document ready?
For example:
$(function(){
$('select').jQueryExample();
nativeExample();
});
$.fn.jQueryExample = function(){
//Do something
}
function nativeExample(a, b)
{
return a + b;
}
As opposed to this where they are called AND defined in document ready?
$(function(){
$('select').jQueryExample();
nativeExample();
$.fn.jQueryExample = function(){
//Do something
}
function nativeExample(a, b)
{
return a + b;
}
});
::EDIT::
One extra question. If a function is defined outside document ready and also called outside document ready, what would happen as opposed to having it defined outside but called inside document ready?
I am asking this because I have a function defined outside the document ready scope and this function is an ajax call which gets new messages on page load. Should it be called outside or inside document ready?
For example:
$(function(){
//Hello, I am jQuery
});
nativeExample();
function nativeExample(a, b)
{
return a + b;
}
as opposed to:
$(function(){
nativeExample();
});
function nativeExample(a, b)
{
return a + b;
}
Thanks in advance for replying.
I think you should defined your functions outside the jQuery
readymethod, because:readyevent, you can’t do it if the function is defined inside the event,readymethod should be used only when it’s really needed, it means when you *really have to wait for the DOM to be readyFor information, here is a simplified but common HTML page pattern that I use every time, it works pretty well:
The
jquery.plugins.jsfile could contains something like you provided:The file
yourcode.jscould be:About your edit, your question
what would happen as opposed to having it defined outside but called inside document readydoesn’t have a universal answer. Look at this example:The function
getFoodoesn’t need the DOM to work. So, its 4 calls always returns ‘foo’, so the function works wherever and whenever she was called (inside or outside the ‘ready’ event).The function
getAnchorquery the DOM and return a collection of the anchor tag in the page. The first call, in the script “ONE”, is outside thereadyevent and returns undefined. The third call, in the script “THREE”, is also outside thereadyevent but it logs an array of anchor elements in the console. This means that, the placement of the function call can alter the function behavior. In the first call, obviously the anchor tag was not present in the page, and that’s why the function returnsundefined. Then, the second call and fourth call, placed at the beginning and at the end of the page, both logs an array. In this case, the placement of the function call doesn’t alter the function behavior, because the functiongetAnchoris actually called after all the DOM elements have been loaded. If you look at your console logs, you’l see something like this:Look at the log order: one, three, two, four; which is different from the source order: one, two, three, four. Functions is the
readyhave been delayed until the page loading is complete.