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

I have an interesting question that may sound pretty silly, but here it goes. Using the jQuery ready function, I defined some functions, for example:

$(function(){

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

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

For some reason, for IE to display what I'm using correctly, this must be done in the $ (document) .ready () function. However, I need to call these functions when I have a dataset from the server. So I thought I would do something like this ...

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

... will be placed outside the ready function in the script so that I can call it directly.

, , , , , !. , , . - ! $(document).ready() global?

+5
3

( ) inline $().ready, :

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) { };
});
+6

, . , , - , , , . , .

// 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();
});
+6

What about

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

   $(function(){
      // if you need to call inside ready
      function1();
      function2();
   });
+3
source

All Articles