Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

functions inside or outside jquery document ready

Up until now I just put all my jQuery goodness inside the $(document).ready() function, including simple functions used in certain user interactions.

But functions that don´t require the DOM document to be loaded or are only called afterwards anyway, can be placed outside the $(document).ready() as well. Consider for example a very simple validation function such as:

function hexvalidate(color) {
// Validates 3-digit or 6-digit hex color codes 
 var reg = /^(#)?([0-9a-fA-F]{3})([0-9a-fA-F]{3})?$/;
 return reg.test(color);
}

The function is only called from within the $(document).ready() function though.

What is best practice (syntax, speed); placing such a function inside or outside the jquery document ready function?

like image 513
Hans Avatar asked Apr 15 '10 12:04

Hans


People also ask

Can I 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.

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

What is difference between $( function () and document Ready?

There is no difference in functionality between your examples - they both bind to DOM ready. For reference, there are two points at which you can bind your jQuery code. The first will execute when the DOM is ready (both are equivalent): // full example $(document).

Why are jQuery methods written inside a document ready event?

A simple solution for anyone learning jQuery is to always declare your jQuery code inside the document ready function. This is to ensure that all of the html page elements also known as the DOM (Document Object Model) have loaded before any jQuery code is run.


2 Answers

Put it inside so it won't pollute the global namespace. It also ensures a faster resolve of the function name because of JavaScript's scope chains.

Put it outside if it's a reusable component so you could easily move it in a separate file and call from different contexts.

Since you already use JQuery, it's worth mentioning, that in your case you may want to define hexvalidate as a JQuery plugin outside and then invoke it inside.

like image 120
viam0Zah Avatar answered Oct 09 '22 14:10

viam0Zah


I don't think you should be using any 'just functions' in the first place. In OOP javascript a "function" usually belongs to one of four distinct types:

  • Constructor or an anonymous 'init' closure - used to construct objects. The only type of function that is allowed to be global
  • Method - function that is a part of some object
  • Utility - inner function of a constructor/method, invisible from outside
  • Constant - a functional constant passed as a parameter

e.g.

 (function() { <- init closure

        function helper1() { <- utility }

        globalSomething = {

              foobar: function() { <- method 
                  xyz.replace(/.../, function() { <- constant })

              }
        }
  )()

In your example, 'hexvalidate' is obviously a part of Validator object, which, in turn, can be made a jQuery plugin:

   (function($) {
        $.validate = {
            hexColor: function(color) { ... your code }
            more validators...
        }
     )(jQuery)
like image 10
user187291 Avatar answered Oct 09 '22 14:10

user187291