Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper use of jquery $(document).ready() when declaring functions called within ready()

Tags:

jquery

I have gotten into the habit of starting jquery coding with the ready function $(function(){...}); and putting all functions called from ready within ready.

Then I realized that some of the functions put into the ready function probably didn't need to be there.

For example, simple functions used by events in document ready could be declared outside of it:

function checkEmail(objelement){
   var emailRx = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
   return emailRx.test(objelement.val()) ? true : false;
}

Then used inside of it:

//code snippet for example

$(function(){

$("form[name='contactform']").submit(function(){
    $("input[type=text]").each(function(){
        if($(this).attr("id") == "email" && !checkEmail($(this))) { 
            $(this).prev().css("color","red");
       }
    });
});

});

I searched through many SO previous questions and couldn't seem to find an answer.

Is it better, worse, or no different to declare functions outside of ready in this manner?

like image 949
jk. Avatar asked Jul 28 '11 15:07

jk.


People also ask

What is the purpose of $( document ready () function in jQuery?

The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.

How do you call a function in document ready jQuery?

$( document ). ready() A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ).

Why do we start our code with document ready () in jQuery?

ready() function will load as soon as the DOM is loaded and before the page contents are loaded. You should wrap all your javascript code with this function to ensure that the code only runs when the page is fully rendered.

Can we write function inside $( document ready?

Yes, you can do that, it's just a matter of scope. If you only need to access callMe() from within $(document). ready(function() { }) , then it's fine to put the function there, and offers some architecture benefits because you can't access the function outside of that context.


1 Answers

If you declare them outside, all it means is that they exist in the global scope.

An advantage of this is that they can be reused outside of the document.ready function. A disadvantage is that they clutter up the global namespace (this can be avoiding by namespacing, if you want).

Personally, if I'm going to reuse them, they go outside. If they need to reference a variable or something else that will only exist within the scope of my domready handler, they go inside (usually as anonymous functions though).

Lately, I've been putting everything I can outside of the domready handler simply because the interpreter has to parse all function declarations within the domready handler before starting to execute the handler and delaying execution of your domready handler sort of defeats the purpose (regardless of how negligible that time is).

like image 165
14 revs, 4 users 99% Avatar answered Sep 20 '22 12:09

14 revs, 4 users 99%