Firebug Tutorial (Section 3): Script Tab :Javascript Debugging

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Firebug Tutorial – Script Tab : Javascript Debugging

September 30, 2007

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.

  1. Overview of Script Tab
  2. Debugging Javascript with Firebug
  3. Javascript File Selector
  4. Conditional breakpoint
  5. Using Commandline API while debugging
  6. 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.

Firebug - Script Tab

  1. 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.
  2. 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.)
  3. Line of Code & breakpoint : This is a place where you can set the breakpoint for debugging.
  4. 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.
  5. 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)

    breakpoint-on-fb.jpg

  • Take a look at “Breakpoint” window at the right panel. (One line is added in “Breakpoints” windows as shown in pic.)

    breakpoint-window.jpg

  • Click “Click Me!” button on your page. (The Javascript execution will stop at the breakpoint that you set on line 7. )

    break-on-line.jpg

  • 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.

    step-out.jpg
    (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. )

    watch-window.jpg

  • 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.

conditional-breakpoint.jpg

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.

consoledir-debug.gif

#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.

  1. 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.
  2. 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.
  3. 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..

Share
Firebug Tutorial – Script Tab : Javascript Debugging

tags: Ajax debugging, Firebug, Firefox Addon, Firefox Extension, Javascript debugging
posted in Firebug, Firefox by Michael Sync

转载于:https://my.oschina.net/xcntime/blog/6542

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/268142.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ObserveIT Client安装后屏幕延迟问题

前天几遇到一个很是郁闷的问题: 问题&#xff1a;当把ObserveIT服务器端安装好后&#xff0c;在其他XenAPP服务器上安装Client端时&#xff0c;一旦安装后&#xff0c;Client端就会出现屏幕延迟问题&#xff08;屏幕、菜单会卡1秒钟左右&#xff09;&#xff0c;最初以为是与服…

mysql规范中每条命令可以使用; \g \G 结尾

select * from 表名; select * from 表名\g select * from 表名\G

线性直接变换方法对摄像机进行标定

具体实现过程如下&#xff1a; 1.从不同角度用摄像机对立方体盒子进行拍摄&#xff0c;得到一幅图像。如下&#xff1a; 2.在立方体盒子上选取至少6个点&#xff0c;获取这6个点的图像坐标和空间点坐标&#xff08;世界坐标系自己设定&#xff09;。如&#xff1a; 7 %点个数 0…

图灵2010.09书讯

图灵五周年活动汇集 &#xff1a; 【互动】图灵五周年盛大庆典 数千好书免费送 【互动】图灵五周年——纪念国际知名的UNIX和网络专家Stevens 【互动】图灵五周年——精品数学书买赠活动 【卓越】图灵五周年——纪念国际知名的UNIX和网络专家Stevens 【卓越】图灵5周年专题…

mysql中使用WITH ROLLUP

1:例子&#xff0c;有一张book表 2:执行sql求和 SELECTsum(age) FROMstudent GROUP BYage; 3:加上with ROLLUP 查询结果会加上一行&#xff0c;显示总和 SELECTsum(age) FROMstudent GROUP BYage with ROLLUP;

php写的squid验证辅助器

2019独角兽企业重金招聘Python工程师标准>>> php写的squid验证辅助器2008-11-08 23:17公司的代理服务器用的是squid&#xff0c;基于IP地址和MAC地址进行权限验证允许部分用户访问Internet。无奈列位高手们早已通晓盗用IP、MAC的方法来绕过squid的限制。近来考虑改为…

动手动脑 - 继承与多态

1.运行 TestInherits.java 示例&#xff0c;观察输出&#xff0c;注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码&#xff0c;显式调用GrandParent的另一个构造函数&#xff0c;注意这句调用代码是否是第一句&#xff0c;影响重大&#xff01; 通过 super …

SilverlightCMS开发之3经典三页面CMS浮现

随笔思路&#xff1a; CMS粗略介绍 CMS具体说明 一个ListBox的效果实例 一些RIA Service 实例 那个SL CMS 的效果。 下一步1CMS粗略介绍 1本来昨晚就弄好了的。今天又改了一天&#xff0c;加入了我去年写的一个CMS做后台管理和与数据库交互。 这一下整个东西在功能上就略显完整…