Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make $(document).ready() functions available globally?

I have an interesting question here that may sound pretty silly, but here goes. Using jQuery's ready function I have defined some functions like so:

$(function(){

  var function1 = function(data){
    //do something
  }

  var function2 = function(data){
    //do something else
  }
});

For some reason, in order for IE to render what I am using correctly, it must be done in the $(document).ready() function. However, I need to trigger these functions once I have a dataset from the server-side. So I thought I would do something like this:

Object.Namespace.callFunction = function(data){
 function1(data);
}

...to be placed outside the ready function in a script so that I could call it directly.

Unfortunately, I know this doesn't work because well, it does not seem logical and I have tried it!. I made all these functions arbitrary because it does not matter the content, but rather the concept. I have also tried using event handlers to trigger the function once I get that data -- to no avail! What is the best way to make functions inside the $(document).ready() global?

like image 257
sctskw Avatar asked Mar 11 '10 22:03

sctskw


People also ask

Can we use multiple document ready () function on the same page?

Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.

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

What is $( document ready () function Why should you use it?

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.

What is $( document ready () equivalent in JavaScript?

jQuery $(document). ready() Equivalent in JavaScript This event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.


3 Answers

If you are defining global functions there is no reason to have them in the document ready. The only things that should go in the document ready are things that need to wait until the document is ready in order to act. Defining function can happen before the document is ready.

// Defining the functions in the global scope.
var function1 = function(data){
    //do something that requires the dom to be ready.
}

var function2 = function(data){
    //do something else that requires the dom to be ready.
}
$(function() {
    // Running the functions when the document is ready.
    function1();
    function2();
});
like image 148
PetersenDidIt Avatar answered Nov 15 '22 07:11

PetersenDidIt


If you (for stylistic reasons) want to write the function inline with your $(document).ready, You can do it this way:

var app={}; /*Use whatever your apps name is, abbreviated (something short)*/
$(function()
{
  app.function1 = function(data) { };
  app.function2 = function(data) { };
  // now you can call all functions inside and outside this ready function with the app. prefix
  // if you also want a local reference to the function without the app. prefix, you can do:
  var function1 = app.function1 = function(data) { };
});
like image 36
Plynx Avatar answered Nov 15 '22 06:11

Plynx


what about

  function function1(data){
    //do something
  }
  function function2(data){
    //do something else
  }

   $(function(){
      // if you need to call inside ready
      function1();
      function2();
   });
like image 37
Jhonny D. Cano -Leftware- Avatar answered Nov 15 '22 07:11

Jhonny D. Cano -Leftware-