Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6190911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:40:17+00:00 2026-05-24T02:40:17+00:00

I have gotten into the habit of starting jquery coding with the ready function

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:40:18+00:00Added an answer on May 24, 2026 at 2:40 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have gotten into the habit of declaring structs nested inside the class that
One of the practices I have gotten into in Python from the beginning is
I've gotten into a habit of using the standard register->send activation email->activate account process
When validating data, I've gotten into a habit of doing the following: Note: I
I'm learning Ruby and have just gotten into some stuff about arrays and ranges.
Suddenly doing updates into a few tables have gotten 10 times slower than they
for the last couple of weeks I've finally gotten into Obj-C from regular C
I've gotten into the habit of marking outlying data by changing cell styles. I'd
I have gotten myself into a tangled mess with quotes. I am trying to
I've seemed to have gotten myself into a bit of a pickle. I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.