Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling custom functions from firebug console

I created a document that loads a .js document that has some JQuery functions that I have created. I would like to use the firebug console to quickly test the functionality of these functions on my html document. But when I try and call these function in the console I don't get any response.

for example:

  • I have index.html that call my JS:

    <script src="jquery.js" type"text/javascript"></script>
    <script src="myfunctions.js" type="text/javascript"></script>
    
  • Myfuntions.js has the following defined in it:

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

The 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 directly I get the expected behavior.
What is the proper way to call a user defined function from the console?

like image 312
E.E.33 Avatar asked Feb 19 '12 23:02

E.E.33


People also ask

How do I run a console command in Firefox?

To execute the snippet that is currently in the editing pane, click the Run button or press Ctrl + Enter (or Cmd + Return on MacOS).

How do you call a function in browser console?

Press Command+Option+J (Mac) or Control+Shift+J (Windows, Linux, ChromeOS) to open the Console, right here on this very page.

What is the function of console in JavaScript?

It enables us to interact with a web page by executing JavaScript expression in the contents of the page. In JavaScript, the console is an object which provides access to the browser debugging console. We can open a console in web browser by using: Ctrl + Shift + I for windows and Command + Option + K for Mac.


2 Answers

If showAbout is defined at the global scope, you should be able to write showAbout(); in the console and see the result. If not, then you're probably putting your functions in a scoping function like so:

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

If so, good for you, you've avoided created global variables/functions. But it does mean you can't access those functions from the console, because the console only has access to global scope.

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

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

That explicitly puts a showAbout property on the global object (window) referencing your function. Then showAbout(); in the console will work.

like image 113
T.J. Crowder Avatar answered Sep 30 '22 02:09

T.J. Crowder


Your showAbout() probably isn't in the global scope.

It may be wrapped by a $(document).ready() code, and its scope limited to that function.

For testing, you could add beneath...

window.showAbout = showAbout;

You can then call showAbout() from your console, once the function has been defined.

like image 45
alex Avatar answered Sep 30 '22 01:09

alex