In Chrome's JavaScript console, how do I call a function that belongs to a .js file included in the webpage I am viewing?
在Chrome的JavaScript控制台中,我如何调用一个属于我正在查看的网页中的.js文件的函数?
4 个解决方案
#1
55
If it's inside a closure, i'm pretty sure you can't.
如果它在闭包里,我很确定你不能。
Otherwise you just do functionName();
and hit return.
否则只执行functionName();点击返回。
#2
16
An example of where the console will return ReferenceError is putting a function inside a JQuery document ready function
控制台将返回ReferenceError的一个示例是将函数放入JQuery文档准备函数中
//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.
当然,如果需要在加载文档之后注册函数,就不能这样做。但你也许能解决这个问题。
#3
5
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)].
这是一个旧的线程,但我只是搜索并找到了它。我对使用Web开发工具很陌生:主要是Firefox开发工具(Firefox v51),还有Chrome DevTools (Chrome v56)。
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
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).
我还添加了Chrome“Scratch JS”扩展:看起来它提供的功能与Firefox开发工具(下面的屏幕截图)中的“刮板”功能相同。
https://chrome.google.com/webstore/detail/scratch-js/alploljligeomonipppgaahpkenfnfkn
https://chrome.google.com/webstore/detail/scratch-js/alploljligeomonipppgaahpkenfnfkn
Image 1 (Firefox): http://imgur.com/a/ofkOp
图1(Firefox):http://imgur.com/a/ofkOp
Image 2 (Chrome): http://imgur.com/a/dLnRX
图2(铬):http://imgur.com/a/dLnRX
#4
1
you can invoke it using window.function_name() or directly function_name()
可以使用windows .function_name()或直接function_name()调用它
#1
55
If it's inside a closure, i'm pretty sure you can't.
如果它在闭包里,我很确定你不能。
Otherwise you just do functionName();
and hit return.
否则只执行functionName();点击返回。
#2
16
An example of where the console will return ReferenceError is putting a function inside a JQuery document ready function
控制台将返回ReferenceError的一个示例是将函数放入JQuery文档准备函数中
//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.
当然,如果需要在加载文档之后注册函数,就不能这样做。但你也许能解决这个问题。
#3
5
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)].
这是一个旧的线程,但我只是搜索并找到了它。我对使用Web开发工具很陌生:主要是Firefox开发工具(Firefox v51),还有Chrome DevTools (Chrome v56)。
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
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).
我还添加了Chrome“Scratch JS”扩展:看起来它提供的功能与Firefox开发工具(下面的屏幕截图)中的“刮板”功能相同。
https://chrome.google.com/webstore/detail/scratch-js/alploljligeomonipppgaahpkenfnfkn
https://chrome.google.com/webstore/detail/scratch-js/alploljligeomonipppgaahpkenfnfkn
Image 1 (Firefox): http://imgur.com/a/ofkOp
图1(Firefox):http://imgur.com/a/ofkOp
Image 2 (Chrome): http://imgur.com/a/dLnRX
图2(铬):http://imgur.com/a/dLnRX
#4
1
you can invoke it using window.function_name() or directly function_name()
可以使用windows .function_name()或直接function_name()调用它