如果您对javascript–chrome是firebug评估控制台的替代品吗?和chromedebugjs感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解javascript–chrome是f
如果您对javascript – chrome是firebug评估控制台的替代品吗?和chrome debug js感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解javascript – chrome是firebug评估控制台的替代品吗?的各种细节,并对chrome debug js进行深入的分析,此外还有关于Chrome 控制台 如何调试 javascript、Chrome控制台快速查找调试JavaScript运行代码、Firebug & Chrome Console 控制台使用指南、Firebug Tutorial (Section 3): Script Tab :Javascript Debugging的实用技巧。
本文目录一览:- javascript – chrome是firebug评估控制台的替代品吗?(chrome debug js)
- Chrome 控制台 如何调试 javascript
- Chrome控制台快速查找调试JavaScript运行代码
- Firebug & Chrome Console 控制台使用指南
- Firebug Tutorial (Section 3): Script Tab :Javascript Debugging
javascript – chrome是firebug评估控制台的替代品吗?(chrome debug js)
我最喜欢调试嵌套表达式的技术之一是放置一个断点,在控制台选项卡的firebug输入面板中输入一个表达式,然后按下运行.
但是我没有在chrome中看到这个功能.我可能忽略了一些东西.它在那里吗?我在哪里可以找到它.
解决方法
Chrome 控制台 如何调试 javascript
转自 http://www.cnblogs.com/ctriphire/p/4117370.html
下面简单介绍一下如何利用好chrome控制台这个神器好好调试javascript代码
先说一下源码定位
大家打开测试网页 看到页面右下方有一个推荐的图标吗?右击推荐图标,选择审查元素,打开谷歌控制台,如下图所示
我们现在想知道votePost方法到底在哪?跟着我这样做,在Console面板里面输入votePost然后回车
直接点击上图标红的链接,控制台将定位到Sources面板中,展示如下图所示
大家看了上面这个图片之后估计头都要晕了吧,这么多js都整在一行,让人怎么看呀,不用担心,按下图操作即可(也就是点击中间面板左下方的Pretty print就行了)
这时我们再回到Console面板时会惊奇的发现原来的链接后面的1现在变成91了(其实这里的数字1或者91就是代表votePost方法在源码中的行号 )现在看出Pretty print按钮的强大之处了吧
知道了怎么样查看某一个按钮的源码,那接下来的工作便是调试了,调试第一步需要做的便是设置断点,其实设置断点很简单,点击一下上图所示的92即可,这时你会发现92行号旁边会多了一个图标,这里解释一下为什么不在91处设置断点,你可以试下,事实上根本就没法在91处上设置断点,因为它是函数的定义处,所以没法在此设置断点。
设置好了断点后,你就会在右边Breakpoints方框里看到刚刚设置的断点。
我们先来介绍一下用到的调试快捷键吧(事实上我们也可以不用下表所示的快捷键,直接点击上图所示右侧栏最上层的一排按钮来进行调试,具体用哪个按钮,把鼠标放到按钮上方一会就会显示它相应的提示)
快捷键 | 功能 |
---|---|
F8 | 恢复运行 |
F10 | 步过,遇到自定义函数也当成一个语句执行,而不会进入函数内部 |
F11 | 步入,遇到自定义函数就跟入到函数内部 |
Shift + F11 | 步出,跳出当前自定义函数 |
其中值得一提的是,当我们点击“推荐”按钮进行调试的时候会发现,不管我们是按的F10进行调试还是按F11进行逐步调试,都没法进行$.ajax函数内部,即使我们在函数内部设置了断点也没有办法进入,这里按F8才是真正起效果的,不信你试试。
当我们在调试的时候,右侧Scope Variables里面会显示当前作用域以及他的父级作用域,以及闭包。你不仅能在右侧 Scope Variables(变量作用域) 一栏处看到当前变量,而且还能把鼠标直接移到任意变量上,就可以查看该变量的值。
用图说话(哈哈)
刚刚我们介绍的只是在html里面能够看得到它绑定了onclick事件,这样我们就找到它绑定的js函数,如果它是在jQuery页面加载完成函数里面绑定的,这时候我们怎么知道它绑定的是哪个js函数呢,如果我们不知道绑定的js函数就更加不用说调试进去了
下面介绍一下如何查看,还是以刚刚那个测试网页为例子吧,但是这次我们来看“提交评论”作说明吧,
右击“提交评论”-->审核元素,我们可以清楚的看到在这个按钮上未绑定任何事件。在Console面板内输入如下代码
function lookEvents (elem) { return $.data ? $.data( elem, "events", undefined, true ) : $._data( elem, "events" ); } var event = lookEvents($("#btn_comment_submit")[0]); // 获取绑定的事件
如下图所示:
按照上述介绍的方法定位到具体的blog-common.js里面,找到postComment 然后一层层的找到具体的代码,再设置断点就好了。
最后介绍一下一个神器,很好用的debugger
如果你自己写的代码,你执行的时候想让它在某一处停下来,只要写上的debugger就好了,不信你试试!哈哈
Chrome控制台快速查找调试JavaScript运行代码
下面是小编 jb51.cc 通过网络收集整理的代码片段。小编小编现在分享给大家,也给大家做个参考。
个人觉得 Chrome 是目前最好用的浏览器,IE 简直就是搅屎棍。不知道大家在看别人写的网页时,怎么找对应的 js 动作的?是不是根据 id 或者 name 等标识,在 js 文件中挨个搜索?这种办法是最基础的,最原始的。不过对于大型网站,引用的 js 文件一大片的那种,不知道各位还有没有心情查找。Chrome 内核给我们提供了非常强大的开发人员工具,使用它,我们可以提升很高的效率,下面就跟我一起学习下 Chrome 控制台快速查找调试 JavaScript 运行代码吧!以本站为例,点击用户中心修改用户的昵称,我们可以这样做。
Chrome 控制台必须使用 Chrome 内核才有,目前市面上,除了 IE 之外几乎都有,按 F12 即可开启。选择 Chrome 控制台中 sources 资源选项卡,打开 debugger 窗口。选择 event listener breakpoints 下的 Mouse 下的 click 事件,如下图所示:
点击按钮后,会执行一步 JavaScript,并且暂停,出现 js 调试效果。这个时候如果跳到 jquery 等第三方封装库中,我们可以使用 blackBox script 功能,将其加入黑箱子中,这样就不会暂停到第三方类库中。
如果第一步暂停效果不是你要的代码,你可以点击继续调试按钮,进入下一步 js 执行位置,在资源窗口可以看到对应的代码,如下图:
仅仅只需要点击几下鼠标,就能快速找到网页中执行的 js 代码所在文件及行号,是不是非常方便呢?
以上是小编(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得小编网站内容还不错,欢迎将小编网站推荐给程序员好友。
Firebug & Chrome Console 控制台使用指南
Console API
当打开 firebug (也包括 Chrome 等浏览器的自带调试工具),window 下面会注册一个叫做 console 的对象,它提供多种方法向控制台输出信息,供开发人员调试使用。下面是这些方法的一个简单介绍,适时地运用它们,对于提高开发效率很有帮助。
console.log(object[, object, ...])
使用频率最高的一条语句:向控制台输出一条消息。支持 C 语言 printf 式的格式化输出。当然,也可以不使用格式化输出来达到同样的目的:
var animal=''frog'', count=10;
console.log("The %s jumped over %d tall buildings", animal, count);
console.log("The", animal, "jumped over", count, "tall buildings");
console.debug(object[, object, ...])
向控制台输出一条信息,它包括一个指向该行代码位置的超链接。
console.info(object[, object, ...])
向控制台输出一条信息,该信息包含一个表示“信息”的图标,和指向该行代码位置的超链接。
console.warn(object[, object, ...])
同 info。区别是图标与样式不同。
console.error(object[, object, ...])
同 info。区别是图标与样式不同。error 实际上和 throw new Error() 产生的效果相同,使用该语句时会向浏览器抛出一个 js 异常。
console.assert(expression[, object, ...])
断言,测试一条表达式是否为真,不为真时将抛出异常(断言失败)。
console.dir(object)
输出一个对象的全部属性(输出结果类似于 DOM 面板中的样式)。
console.dirxml(node)
输出一个 HTML 或者 XML 元素的结构树,点击结构树上面的节点进入到 HTML 面板。
console.trace()
输出 Javascript 执行时的堆栈追踪。
console.group(object[, object, ...])
输出消息的同时打开一个嵌套块,用以缩进输出的内容。调用 console.groupEnd() 用以结束这个块的输出。
console.groupCollapsed()
同 console.group(); 区别在于嵌套块默认是收起的。
console.time(name)
计时器,当调用 console.timeEnd(name);并传递相同的 name 为参数时,计时停止,并输出执行两条语句之间代码所消耗的时间(毫秒)。
console.profile([title])
与 profileEnd() 结合使用,用来做性能测试,与 console 面板上 profile 按钮的功能完全相同。
console.count([title])
输出该行代码被执行的次数,参数 title 将在输出时作为输出结果的前缀使用。
console.clear()
清空控制台
控制台的输出面板右边,是控制台的输入面板(Chrome 调试工具对应为下方),在这里除了可以运行常规的 javascript 代码,还内置了相当数量的命令行可以辅助我们的调试工作,下面是一些常用命令行的简单介绍。
$(id)
返回一个给定 id 的元素。
$$(selector)
返回给定的 css 选择器匹配到的一组元素。
$x(xpath)
返回给定的 XPath 表达式匹配到的一组元素。
$0
在 HTML 面板中选中的元素。
$1
上一次在 HTML 面板中选中的元素。
$n(index)
访问最近 5 个被选中过的元素,index 的范围: 0 – 4。
dir(object)
同 console.dir(object)。
dirxml(node)
同 console.dirxml(node)。
clear()
同 console.clear()。
inspect(object[, tabName])()
在合适的(或一个指定的) tab 中检视一个对象。
keys(object)
返回一个对象的所有属性的键。
values(object)
返回一个对象的所有属性的值。
debug(fn)
在函数第一行添加一个断点,使用 undebug(fn) 移除断点。
monitor(fn)
开启一个函数的调用日志,使用 unmonitor(fn) 关闭该功能。非常有用的一个命令,但是它似乎并没有很好地工作。
monitorEvents(object[, types])
开启一个元素的某个事件(或所有事件)被触发时的日志记录。用例如下:
monitorEvents($0,[''click''])
上面的命令行被执行后,将开启当前在 HTML 面板中被选中元素的 click 事件监控,一旦这个元素的 click 事件被触发,事件对象将会在控制台输出。如果不指定第二个参数,将对所有事件进行记录。
profile([title])
同 console.profile([title])
Firebug Tutorial (Section 3): Script Tab :Javascript Debugging
Firebug Tutorial – Script Tab : Javascript Debugging
Firebug Tutorial
Section 3: Script Tab : Javascript Debugging
I’m going to show you how to debug the Javascript code with Firebug in this tutorial. If you are an Ajax developer, the tutorial will help you in many ways to boost your production in your RIA (Rich Internet Application) development.
The following topics will be covered in this tutorial.
- Overview of Script Tab
- Debugging Javascript with Firebug
- Javascript File Selector
- Conditional breakpoint
- Using Commandline API while debugging
- debug(fn) and undebug(fu) <CommandLine API>
Download : Sample File
Note : Please refresh your web page if something goes wrong in Firebug console while you are debugging. As I’m using Firebug for my web project over 1 year, I know that this tool is very useful too. However, there are a few issues and limitations with this tool. So, please don’t mind about that. If you found any bug, you can report here. OR, you can search the existing issue in issue list.
#1. Overview of Script Tab
The Script tab is the fourth tab of Firebug that allows you to debug the Javascript code on the browser. There are two subpanels in script panel. The panel on the left is the Javascript editor for debugging the javascript code. The subpanel on the right includes two sub panels called “Watch” and “breakpoint”. Please take a look the picture and description in below for more details.
- JS Editor : This is the Javascript editor where you can debug the Javascript. There is one option called “Break on All Errors” in this editor. If you check this option, the script exection will be paused if the errors occurs in your script.
- JS File Selector : If you click on it, you will see the list of all Javascript files that are included in your page. (Please check “#3. Javascript File Selector” section for more details.)
- Line of Code & breakpoint : This is a place where you can set the breakpoint for debugging.
- Watch Window: It displays the value of variables as a list in that window. If you have some experiences in using Microsoft Visual Studio, you already have some idea about how to use Watch window. There is only one difference between the Watch window from Firebug and the one from Visual Studio. In Visual Studio, the “Watch” window displays the value of selected variables. But the “Watch” window of Firebug will display all values of variables within the current scope.
- list of breakpoints : The list of breakpoints that you set in Javascript Editor will be shown in that panel. You can also remove all breakpoints from that panel.
#2. Debugging Javascript with Firebug
Debugging javascript is very straightforward process with Mozilla Firefox and Firebug. If you are Visual Studio developer then you won’t feel any differences while you are debugging the Javascript code with Firebug excepts the debugger runs as the part of browser. Let’s follow the steps to take a look how to debug the JS code.
Steps to debug Javascript with Firebug
- Copy the code below and paste them in notepad and save as a htm file. ( or If you already downloaded the sourcecode of this article, you can find the html file called JS-Example1.htm in zip file. )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Javascript Debugging with Firebug</title>
<script type="text/javascript">
function doSomething(){
var lbl = document.getElementById(''messageLabel'');
lbl.innerHTML = "I just did something.";
}
</script>
</head>
<body>
<div>
<div id="messageLabel"></div>
<input type="button" value="Click Me!" onclick="doSomething();" />
</div>
</body>
</html>
Example 1.0
- Open your HTML file in Firefox browser.
- Launch the Firebug console and go to “Script” tab.
- Set the breakpoint on line 7 (as shown in pic below)
- Take a look at “Breakpoint” window at the right panel. (One line is added in “Breakpoints” windows as shown in pic.)
- Click “Click Me!” button on your page. (The Javascript execution will stop at the breakpoint that you set on line 7. )
- You can step thought the code by using one of those buttons (Continue, Step Over, Step Into and Step Out ) on the toolbar of Firebug.
(From left to right)- Continue (F8) : allow you to resume the script execution once it has been stopped via breakpoint
- Step Over (F10) : allow you to step over the function call.
- Step Into (F11) : allow you to step into the body of the another function.
- Step Out : allow you to resume the script execution and will stop at next breakpoint.
- So, click “Step Over” icon to go to the next line (line 8). (then, please take a look at “Watch” window. The values of variable called “lbl” will be displayed on “Watch” window. )
- then, Click “Step Over” icon to go to the next line. As there is no next line in our script, the execution will be stopped.
Yeah. That’s all about simple Javascript debugging with Firebug. I will explain about more advanced features such as “using conditional breakpoint” and “using commandline API while debugging” in next example.
Note about “Watch”window : Even though the value of the most variables are shown in “Watch” window, there might be some cases that you can’t find the variable you want in “Watch” window. In that case, the Commandline API are very helpful for you.
#3. Javascript File Selector
Using firebug console, you can easily find out how many script files are included in your page. (Please check-out the picture below. ) And also, you can change the script file that you wanna to debug.
#4. Conditional breakpoint
The conditional breakpoint is very helpful when you don’t want to debug line-by-line. For example, there is one for-loop that loops 50 times in your code. If you set the normal breakpoint, the execution will be paused each time you enter in that loop. But if you are using conditional breakpoint, you can put the condition on your breakpoint so that the script execution won’t be paused every time you enter in that loop.
In order to show you how to use the conditional breakpoint, I will change the Javascript code as below from the previous example (e.g: 1.0). (If you already downloaded the sourcecode of this tutorial, please take a look the html file called “JS-Example2.htm” in zip file.)
function Dwarf(name){
this.Name = name;
}
function DwarfFactory(){
var dwarfs = new Array();
this.AddDwarfs = function(){
dwarfs[0] = new Dwarf(''Bashful'');
dwarfs[1] = new Dwarf(''Doc'');
dwarfs[2] = new Dwarf(''Dopey'');
dwarfs[3] = new Dwarf(''Grumpy'');
dwarfs[4] = new Dwarf(''Happy'');
dwarfs[5] = new Dwarf(''Sleepy'');
dwarfs[6] = new Dwarf(''Sneezy'');
}
this.ShowDwarfs = function(){
for(var idx in dwarfs){
console.log(dwarfs[idx].Name);
}
}
this.ToString = function(){
var names = '''';
for(var idx in dwarfs){
names += dwarfs[idx].Name + '' '';
}
return names; //dwarfs.join('' '');
}
}
function doSomething(){
var objDwarfFactory = new DwarfFactory();
objDwarfFactory.AddDwarfs();
objDwarfFactory.ShowDwarfs();
var lbl = document.getElementById(''messageLabel'');
lbl.innerHTML = objDwarfFactory.ToString();
}
Example : 1.1
In our example, there is one for-loop in “ShowDwarfs()” function. We will set the conditional breatpoint in that loop. We wanna pause the script execution only when the name of dwarfs object is “Happy”. So, right-click on Javascript editor and put the condition “dwarfs[idx].Name == ‘Happy’” in the properties of breakpoint as shown in screenshot below. Then, press “Enter” key.
then, click the button on your webpage. The script execution will be paused when the condition that we put is true. We can also use the commandline API while debugging.
#5. Using Commandline API while debugging
If you have no idea about Firebug’s commandline API, I suggest you to read this tutorial first. Like we used to use “Immediate” window while we are debugging the code in Visual Studio, you can use Console panel with Commandline APIs while debugging Javascript with Firebug.
Let’s continue the previous example. The execution is paused when the condition is true. Then, you can go to Console panel and type any commandline API to find out more about current dwarf object. Let’s say we typed “console.dir(dwarfs[idx])” in one-line commandline. then, You will get the result “Name “Happy”" in console panel as pic below.
#6. Using debug(fn) and undebug(fu) API
As I said in my Commandline API article, I will explain about debug() and undebug() API here.
#11. debug(fn) and undebug(fu)
Adds or removes a breakpoint on the first line of a function.
Note: I’m not going to cover about this API in this tutorial. Please read more about this in next section.
Basically, debug(fn) and undebug(fn) APIs allows you to set/remove the breakpoint based on the function name from commandline or Javascript code instead of setting the breakpoint in script panel.
Example
- Open the “JS-Example2.htm” from zip file
- Remove all breakpoints that you set earlier. (Script panel>Breakpoint panel>Options>Remove all breakpoints)
- Go to the Console panel.
- Type “debug(doSomething)”
- then, click “Click Me!” button. (Observe: The script execution will be paused at the first line of doSomething() function. )
- If you want to remove the breakpoint that you set, type “undebug(doSomething)” in commandline.
So, keep in mind that there are three ways to set the breakpoint in Firebug.
- Static Breakpoint : It can be set based on line number. You need to set this kinda breakpoint by clicking the line of code bar in Script panel.
- Conditional breakpoint : It can be set based on the condition. You need to set this kinda breakpoint by clicking the line of code bar in Script panel.
- Dynamic breakpoint : It can be set based on the name of Javascript function. You can set this from commandline or Javascript code by using debug(fn) and undebug(fn).
If you wanna try to debug Ajax application, there is one sample file called AjaxExample folder. So, feel free to try debugging this sample if you want.
Okay. that is all about debugging Javascript with Firebug. I hope you will help it useful. Sorry for taking so long to post this tutorial because there are a lot of problems in my country (Myanmar) so I have no mood to blog. :( Anyway, I have tried to cover as much as I can in this tutorial. Feel free to let me know if you have any comment or suggestion. Thanks for reading..

tags: Ajax debugging, Firebug, Firefox Addon, Firefox Extension, Javascript debugging
posted in Firebug, Firefox by Michael Sync
关于javascript – chrome是firebug评估控制台的替代品吗?和chrome debug js的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Chrome 控制台 如何调试 javascript、Chrome控制台快速查找调试JavaScript运行代码、Firebug & Chrome Console 控制台使用指南、Firebug Tutorial (Section 3): Script Tab :Javascript Debugging等相关内容,可以在本站寻找。
本文标签: