Call custom functions from firebug console

I created a document that loads a .js document that has some jQuery functions that I created. I would like to use the firebug console to quickly check the functionality of these functions in my html document. But when I try to call this function in the console, I get no response.

eg:

  • I have index.html that calls my JS:

    <script src="jquery.js" type"text/javascript"></script> <script src="myfunctions.js" type="text/javascript"></script> 
  • Myfuntions.js defines the following:

     function showAbout(){ $('div#about').show("slow"); $('.navbar #about-button').attr('disabled', true); } 

Problem:

When I try to call showAbout or showAbout() from the console on index.html, I don't get any changes. However, when I call $('div#about').show("slow"); or $('div#about').show("slow"); from the console, I get the expected behavior.
What is the correct way to call a custom function from the console?

+4
source share
2 answers

If showAbout defined globally, you should be able to write showAbout(); in the console and see the result. If not, then you probably put your functions in the scope function as follows:

 (function() { function showAbout() { } })(); 

If so, good for you , you avoid creating global variables / functions. But this means that you cannot access these functions from the console, because the console has access only to the global scope.

If you want to export any of them so that you can use them from the console (perhaps only temporarily, for debugging), you can do this as follows:

 (function() { window.showAbout = showAbout; function showAbout() { } })(); 

This explicitly places the showAbout property in a global object ( window ) that references your function. Then showAbout(); The console will work.

+22
source

Your showAbout() is probably not in the global scope.

It can be wrapped with $(document).ready() code, and its scope is limited to this function.

For testing, you can add below ...

 window.showAbout = showAbout; 

You can then call showAbout() from the console as soon as the function is defined.

+2
source

Source: https://habr.com/ru/post/1397333/


All Articles