October 21, 2024
Chicago 12, Melborne City, USA
jQuery

Solving scope issue while using jQuery ready function


How can I solve this scope issue:
I am trying to update an old project.
HTML file includes JS file.
after some code from the JS is running, it calls back a function myFunc() from the HTML.

In the new code, I need to add jQuery ready function to the HTML file.
so I get error

  jQuery.Deferred exception: myFunc is not defined 
  

what is the minimum change I can make in the JS code (or in the HTML code?) to solve this scope issue?

EDIT: all code must be in ready handler, because it depends on document ready

my HTML file:

<html>
<head>
  ... html code ...
  <script src="/my.js"></script>
</head>
<body>
  ... html code ...
  <script>
  $( document ).ready(function() {   // <- this line was added to the new HTML file
    ...
    function myFunc() {              // was called from JS file. but now is considered undefined :(
    ...
  }
  });                                // <- this line was added to the new HTML file
  </script>

</body>
</html>

my JS file:

(function($){
  $(document).ready(function(){
    ...
    myFunc();
    ...
  });

})(this.jQuery);



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video