Calling a Javascript Function from Console

180,862

Solution 1

If it's inside a closure, i'm pretty sure you can't.

Otherwise you just do functionName(); and hit return.

Solution 2

An example of where the console will return ReferenceError is putting a function inside a JQuery document ready function

//this will fail
$(document).ready(function () {
    myFunction(alert('doing something!'));
    //other stuff
})

To succeed move the function outside the document ready function

//this will work
myFunction(alert('doing something!'));
$(document).ready(function () {          
    //other stuff
})

Then in the console window, type the function name with the '()' to execute the function

myFunction()

Also of use is being able to print out the function body to remind yourself what the function does. Do this by leaving off the '()' from the function name

function myFunction(alert('doing something!'))

Of course if you need the function to be registered after the document is loaded then you couldn't do this. But you might be able to work around that.

Solution 3

This is an older thread, but I just searched and found it. I am new to using Web Developer Tools: primarily Firefox Developer Tools (Firefox v.51), but also Chrome DevTools (Chrome v.56)].

I wasn't able to run functions from the Developer Tools console, but I then found this

https://developer.mozilla.org/en-US/docs/Tools/Scratchpad

and I was able to add code to the Scratchpad, highlight and run a function, outputted to console per the attched screenshot.

I also added the Chrome "Scratch JS" extension: it looks like it provides the same functionality as the Scratchpad in Firefox Developer Tools (screenshot below).

https://chrome.google.com/webstore/detail/scratch-js/alploljligeomonipppgaahpkenfnfkn

Image 1 (Firefox): http://imgur.com/a/ofkOp

enter image description here

Image 2 (Chrome): http://imgur.com/a/dLnRX

enter image description here

Solution 4

You can invoke it using

window.function_name()

or directly without window like

function_name()

Solution 5

Basically, there are two cases here:

  1. Your function is in global scope. In that case, simply open a console and call it yourFunction()
  2. Your function is scoped inside some other function(s) and is not accessed globally. In that case, you can open a Sources tab, locate your .js file, place a breakpoint anywhere at the bottom of the outer function (you might need to refresh a page after that if the code have already been run) and call yourFunction() in console. Also, while at breakpoint you may do something like window.yourFuncRef = yourFunction in console, to be able to access it later at any time.
Share:
180,862
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    In Chrome's JavaScript console, how do I call a function that belongs to a .js file included in the webpage I am viewing?

  • lrkwz
    lrkwz over 11 years
    not for me: functionName(); is ok in firebug shows 'undefined' in chrome developer tools.
  • Kevin Ennis
    Kevin Ennis over 11 years
    undefined is the return value of the function. Chrome's dev tools automatically print the return of any function invoked from the console. If it wasn't working, you'd see ReferenceError: functionName is not defined in red.
  • Shah
    Shah over 7 years
    Thanks a lot Tony. Perfect answer
  • Ilan
    Ilan over 6 years
    I tried the 2nd option above, and it almost works, I added a line and placed a breakpoint, and when trying to call my function in the form of var = function, I got an error "Uncaught ReferenceError: showBuy is not defined", but the console itself showed me the function, I just had to tab+enter
  • Alex.Me
    Alex.Me over 6 years
    If you get undefined, this could mean you have placed a breakpoint before the actual definition of a function. maybe you can give more details about how the function is defined and where you set a breakpoint?
  • O'Rooney
    O'Rooney over 2 years
    Nobody could become confused by this behaviour, right? :)
  • Millar248
    Millar248 over 2 years
    @KevinEnnis I thought you were wrong, and then I saw that my css was hiding my change that my js was making. Good catch.