Search code examples
jqueryreferenceerror

Unable to call a jQuery function outside the jQuery(function(){}); body


I am creating a jQuery to create a custom designs. All my functions are running under jQuery(function(){}) body, like:

  jQuery(function(){
       abc();
       function abc(){
          alert('Test');
       }   
  });

This code is working but when I want to call this function on another page after including this file then this give me "ReferenceError: abc is not defined".

Anyone can tell me where I am wrong?


Solution

  • This function is created in an other function, so it is only accessible there.

    you could do something like this:

    var abc;
    jQuery(function(){
    
        abc = function(){
              alert('Test');
        }  
        abc(); 
      });
    

    some where in the page:

    jQuery(function(){
        abc();
    });