Firebug Tutorial – Logging, Profiling and CommandLine (Part I)

Firebug Tutorial

Section 1: Console Tab : Logging, Profiling and CommandLine (Part II)

Overview of Console Tab

This tab is mainly used for logging. It also can be used as CommandLine window (like immediate window in Microsoft Visual Studio) while you are debugging the Javascript. It can be used for monitoring the execution of Javascript code by using Profiling service.

The following topic will be covered in this section.

  • Logging in Firebug (with String Substitution pattern )
  • Grouping the logs or messages
  • console.dir and console.dirxml
  • Assertion ( console.assert() )
  • Tracing ( console.trace() )
  • Timing ( Measuring the time of your code)
  • Javascript Profiler (An introduction in this tutorial, the details will be covered in next tutorial.)

#1. Logging in Firebug

Firebug supports logging in Console tab. So, you don’t need to use alert(‘something’) or document.write(‘something’) anymore.

There are five types of logging in Firebug.

  • console.log : Write a message without icon.
  • console.debug : Writes a message to the console, including a hyperlink to the line where it was called
  • erroricon.png console.error() : Writes a message to the console with the visual “error” icon and color coding and a hyperlink to the line where it was called.
  • infoicon.png console.info() : Writes a message to the console with the visual “info” icon and color coding and a hyperlink to the line where it was called.
  • warningicon.png console.warn() : Writes a message to the console with the visual “warning” icon and color coding and a hyperlink to the line where it was called.

Example Code:

  • Open the htm file called “Plain HTML” or create one HTML file.
  • Paste the following code with <body> tag.
<script language="javascript" type="text/javascript">
console.log('This is log message');
console.debug('This is debug message');
console.error('This is error message');
console.info('This is info message');
console.warn('This is warning message');
</script>

You will get the following output. If you click on hyperlink (“test.htm” in this case), it will take you to script tab and will highlight the line that wrote this message.

basic-logging-concept.jpg

String Substitution Patterns

String substitution parterns can be used in console.log, console.info, console.debug, console.warn and console.error . You can use the same way that we used in C/C++.

%s String
%d, %i Integer (numeric formatting is not yet supported)
%f Floating point number (numeric formatting is not yet supported)
%o Object hyperlink

Example :

Note: I will use console.log in the example below even all console objects (console.log, console.info, console.debug, console.warn and console.error ) support string substitution.

  • Remove “script” tag that we pasted for the previous example.
  • Paste the code below within <body> tag.
<script language="javascript" type="text/javascript">

//This is for normal string substitution " %s, %d, %i, %f".
console.log("My Name is <strong>%s</strong>. My Date of Birth is <strong>%dth %s, %i</strong>. My height is <strong>%f</strong> m.", "Nicolas Cage", 7, 'January', 1964, 1.8542);

function Foo(){
this.LeftHand = function(){
return "Left Hand";
}
this.RightHand = function(){
return "Right Hand";
}
}

//This is for object "%o".
var objFoo = new Foo();
console.log('This is <strong>%o</strong> of Foo class.', objFoo);

</script>

console-string-substitution1.jpg

If you are using %o in your log, the object will be shown as a hyperlink in green color. This hyperlink is linked to the DOM tab. So, If you click “object” in second line, you will see the list of properties of that object (LeftHand and RightHand in this case.)

#2. Grouping

Firebug allows you to group the message or log in Console tab. If you have some many logs in your code, you can probably divide your log into small group or subgroup

Example ~

<script language="javascript" type="text/javascript">

var groupname = 'group1';
console.group("message group : %s " , groupname);
console.log("log message 1 from %s", groupname);
console.log("log message 2 from %s", groupname);
console.log("log message 3 from %s", groupname);
console.groupEnd();

groupname = 'group2';
console.group("message group : %s " , groupname);
console.log("log message 1 from %s", groupname);

var subgroupname = 'subgroup1';
console.group("message group : %s " , subgroupname);
console.log("log message 1 from %s", subgroupname);
console.log("log message 2 from %s", subgroupname);
console.log("log message 3 from %s", subgroupname);
console.groupEnd();

console.log("log message 3 from %s", groupname);
console.groupEnd();

</script>

group-message.jpg

#3. console.dir and console.dirxml

  • console.dir : It can be used for getting all properties and methods of a particular object. According the example below, we can get the Model (property) and getManufactor (method) of Car object by using console.dir(); You can also pass the object of HTML element (eg: console.dir(document.getElementById(‘tbl1’)); ) instead of objCar and let’s see the result. (You will get all properties and methods of the HTML table called “tbl1”).
  • console.dirxml : print the XML source tree of HTML element.
<table id="tbl1" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
</table>
<script language="javascript" type="text/javascript">
//Create a class
function Car(){
this.Model = "Old Model";

this.getManufactor = new function(){
return "Toyota";
}
}

//Create a object
var objCar = new Car();

//Firebug
console.dir(objCar);
console.dirxml(document.getElementById('tbl1'));

</script>

console-dir.jpg

#4. Assertion ( console.assert() )

You can use console.assert() to test whether an expression is true or not. If the expression is false, it will write a message to the console and throw an exception.

Example :

<script language="javascript" type="text/javascript">
function whatIsMyAge(year){
var currYear = 2007;
return currYear - year;
}

var yearOfBirth1 = 1982;
var age1 = 25;
console.assert(whatIsMyAge(yearOfBirth1) == age1);

var yearOfBirth2 = 1982;
var age2 = 11;
console.assert(whatIsMyAge(yearOfBirth2) == age2); //You should get the error here.
</script>

assertion-failure.jpg

#5. Tracing ( console.trace() )

This function is very interesting. Before I tell you the way that I understand, let’s take a look what console.trace does exactly in official website.

console.trace()

Prints an interactive stack trace of JavaScript execution at the point where it is called.

The stack trace details the functions on the stack, as well as the values that were passed as arguments to each function. You can click each function to take you to its source in the Script tab, and click each argument value to inspect it in the DOM or HTML tabs.

This function will tell you about the route information from start point to end point. If you are not clear what I mean, let’s take a look at the sample code and the result.

<!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>Firebug</title>
<script language="javascript" type="text/javascript">
function startTrace(str){
return method1(100,200);
}
function method1(arg1,arg2){
return method2(arg1 + arg2 + 100);
}
function method2(arg1){
var var1 = arg1 / 100;
return method3(var1);
}
function method3(arg1){
console.trace();
var total = arg1 * 100;
return total;
}

</script>
</head>
<body>
<input type="button" value="Trace" onclick="startTrace('Result');"/>
</body>
</html>

trace.jpg

Suppose: we wanna know how “method3” function is invoked. So, we put this code “console.trace()” in that method. then, we run the program and we got the result as picture above. If we read the result from bottom to top, we will see “onclick(click clientX=34, clientY=26)”. That means the execution of Javascript started at on click event of button. then, we got “startTrace(“Result”)” in second line. That means startTrace function is invoked after firing onclick event and the parameter is “Result”. If we keep on checking from bottom to top, we will figure out the completed route from onclick event to method3.

If you wanna test more, you can move this code “console.trace()” to method2(). then, firebug will give the new route from onclick event which is a started point to method2() which is the end point.

I think that it’s pretty useful if you are debugging the other developer’s source code and you have no idea why this function is invoked.

Let me know if you are not clear what I’m trying to explain about console.trace();.

#6. Timing ( Measuring the time of your code)

You can use console.time(timeName) function to measure how long a particular code or function take. This feature is very helpful if you are trying to improve the performance of your Javascript code.

Example :

<!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>Firebug</title>
<script language="javascript" type="text/javascript">
function measuretheTime(){
var timeName = 'measuringTime';
console.time(timeName);

for(var i=0;i&lt;1000;i++){
///do something
for(var j=0;j&lt;100;j++){
//do another thing.
}
}

console.timeEnd(timeName);
}
</script>
</head>
<body>
<input type="button" value="Trace" onclick="measuretheTime();"/>
</body>
</html>

Result : measuringTime: 16ms

#7. Javascript Profiler

You can start the profiler thought code (console.profile(‘profileName’)) or by clicking “Profile” button from “Console” tag. It can be used for improving the performance of Javascript. It is similiar to the console.time() function but profiler can give your more advanced and detailed information.

I will tell you about this more details in next tutorial (Part2) . I hope you all are clear about this tutorial. If you have any comment or suggestion, please drop a comment.. Thanks. C ya tomorrow.

Reference ~


Firebug Tutorial – Overview of Firebug


A few words from me

It has been over 1 year that I’m using Firebug in web development. I found it very useful. I’m really thank to SangSing who introduces me about Firebug. I used to encourage a lot of my techie friends to use Firebug but they said that they don’t know how to use it. They told me that they wanna get something that include all features of firebug and examples. I googled a lit bit but I didn’t find nice resources that covers everything about firebug. So, I decided to write this tutorial. I will try to cover all features of firebug as much as I can. I will show each features with sample sourcecode and screenshot if it required. Don’t hesitate to contact me if you have any suggestion or comment.

There are 5 sections in this tutorial.

  • Section 1: Overview of Firebug : An introduction of Firebug and feature lists, how to install it and a few notes
  • Section 2: Logging, Tracing and CommandLine : Everything related to Console tab. An introduction of CommandLine windows which is the same as Immediate windows or Watch window from Visual Studio.
  • Section 3: HTML DOM Inspector and HTML CRUD operation : This section will tell you about what HTML inspecting is, how to inspect the HTML element, why it is useful and etc. I will also show you how to create/update/delete the HTML DOM dynamically in HTML tab.
  • Section 4: Javascript debugging with Firebug : This section will cover everything about javascript debugging which is the most famous feature of Firebug. It will also show you how to use Console tab while debugging the Javascript.
  • Section 5: CSS, DOM and Net Tab : (I haven’t decided whether I should write about them in one section or not. )

As I said above, I will try to cover as much as possible. If I miss out something in this tutorial, please let me know. I appreciated it.

Getting Started : Overview of Firebug

Note: I will cover some basic understanding about firebug and its features, installation and a few screenshots in this section. So, if you already have some ideas about Firebug, you may probably skip this section.

Firebug - Web Development Evolved

What is Firebug?

Firebug is one of the most popular Mozilla Firefox’s extensions (a.k.a addon). It is also a tool that make the web developers’ life easier. It includes a lot of cool features such as debugging, HTML inspecting, profiling and etc which are very useful for web development.

Features

  • Javascript debugging
  • Javascript CommandLine
  • Monitor the Javascrit Performance and XmlHttpRequest
  • Logging
  • Tracing
  • Inspect HTML and Edit HTML
  • Edit CSS

Where to get Firebug Addon?

  • Go to the official firebug website http://getfirebug.com .
  • There is the image as below at the right-side of the firebug website. Click this image to install.

Click to install Firebug

  • The following dialog will be shown after clicking the image above. Click to “Install Now” button.

Firebug - Software Installation

  • After the installation is completed, you need to restart the browser.

Screenshots

Main Menu (under “Tools” Menu)

“Firebug” and “Error Console” menu will be shown under “Tools” menu of Firefox.

firebug-menu-under-tool-menu.jpg

  • Firebug : It is just a placeholder for Firebug’s submenus.
  • Error Console : If you click this menu, one console window will be launched with the list of errors, warnings and message. Actually, this error console is the same as the console tab from Firebug console.

Firebug Menu

If you click “Firebug” menu under “Tools”, the following submenus will be shown.

firebug-menu-under-firebug-menu.jpg

  • Open Firebug : Open the firebug console within the browser. If you don’t wanna click this menu, you can simply press “F12” to open the firebug console.
  • Open Firebug in New Window : Open the firebug console in separated window.
  • Disable Firebug : It is for disabling firebug. This option is very important. You should disable the firebug if you don’t need it because If you don’t disable the firebug, it might be a lit bit slow to surf the Ajax-enabled websites like GMail.
  • Disable Firebug for **** : You can disable the specific website instead of disabling the whole firebug.
  • Allowed Sites : You can add the list of website that you want to surf with Firebug enabled option.
  • Inspect the Element : This menu is for inspecting the HTML element. Please read more about this in section 3.
  • Profile Javascript : This option is available in Firebug enabled mode only. It is used for monitoring the execution of javascript code. This option is really useful when you have performance issue with your code. Please read more about this in Section 4.
  • Clear Console : Clear the console tab of Firebug console.
  • CommandLine : Open the console tab
  • Search : Set the focus to the Search textbox of current tab.

Content Menu

There is only one menu called “Inspect Element” in content menu. This menu is very useful. Please read more about this in Section 3.

inspect-element-on-content-menu.jpg

Firebug Console

The picture below is Firebug console.

Firebug Console

It contains six tabs such as Console tab, HTML tab, CSS tab, Script tab, DOM tab and Net tab.

  • Console tab : It is for logging, profiling, tracing and commandline.
  • HTML tab : It is for inspecting HTML element, editing HTML and changing the stylesheet at runtime. CSS, Layout and DOM console are already included as the subtabs.
  • CSS tab : You can check how many css file included in the webpage easily. You can simply select the CSS file that you like and you can made the changes to that file on the fly. (I don’t use that tab that much since the same one is already included in HTML tab. )
  • Script tab : It is for debugging Javascript code. Watch and Breakpoints consoles are the subtab of Script tab.
  • DOM tab : It is for exploring DOM. (I don’t use that tab that much. Instead, I used to use DOM tab from HTML console and Script tab.)
  • Net tab : It is for monitoring network activities. It helps you to know why your page (or a particular webpage) is taking so long to load in the browser.

Status Bar

You will see the green cycle if there is no error on your page.

green-cycle-on-status-bar.jpg

You will see the red cycle and the count of errors if there is some errors in your page.

red-cycle-on-status-bar.jpg

Keyboard and Mouse Shortcuts

F12. I used to use only F12 to open/close the Firebug console.

If you wanna read the completed list of shortcuts, you can check-out here.

Problem?

Please check-out this FAQ page.

If you still have problems, you drop a comment in my blog. I will reply as soon as possible. Or you can probably contact with Firebug User Group.

Conclusion

That’s all. :) I think you now have some ideas about what firebug is, how it looks like and how to install. If you are already familiar with firebug, this section will be too plain for you since I didn’t put anything new in this section. I will tell you more details about each tab in next section. So, come back tomorrow!!

Feel free to let me know if you have any idea or comment about this tutorials. Your comment will make this tutorials better. I appreciate it.

8 Steps to create your owned Firefox Extension

This article illustrates how to create a Firefox extension in very simple way. It’s just 8 steps to fellow then you will get some ideas how to create your owned one.I think, it might be useful for those who wanna create their Firefox Extensions.

This article is based on this article from developer.mozilla.org. So, All credits go to the original article “Building an Extension“.

Maybe. you might ask me why I wrote duplicated one.
Well. There are two reasons behind. First thing is that I’m looking a lit bit more details into Firebug Extension so that I have to know how to create an extension for firefox. (At least, simply) and also, I have to take a note all steps so that I don’t need to look the whole article again. Second reason is that the original article was written for all platforms so that it might be a lit bit complicated than this one I wrote. :) All steps in this article are tested in Windows XP sp2 and Firefox 2.0.

Let’s start!

Step #1

Create a folder for your extension somewhere on your hard disk
e.g. C:\extensions\myExtension\

Step #2

Create another folder called chrome under your extension folder
e.g. C:\extensions\myExtension\chrome

Step #3

Create another folder called content under chrome folder
e.g. C:\extensions\myExtension\chrome\content

Step #4

Create two new empty text files, one called chrome.manifest and the other called install.rdf.
e.g. C:\extensions\myExtension\chrome.manifest
e.g. C:\extensions\myExtension\install.rdf

Note: I would suggest to unhide all extensions of all file in your harddisk. If you are not sure how to do it, you can check as follow.

  1. Open “C:\” drive.
  2. Go to Tools->Folder Options.
  3. Go to View Tab.
  4. Uncheck “Hide extensions for known file types

Step #5

Open the file called install.rdf (e.g. C:\extensions\myExtension\install.rdf). Copy the following code and paste it to install.rdf file.

<?xmlversion="1.0"?>

<RDFxmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
  <em:id>sample@foo.net</em:id>
  <em:version>1.0</em:version>
  <em:type>2</em:type>

  <!-- Target pplication this extension can install into,
       with minimum and maximum supported versions.
   --> 
  <em:targetApplication>
   <Description>
    <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
    <em:minVersion>1.5</em:minVersion>
    <em:maxVersion>2.0.0.*</em:maxVersion>
   </Description>
  </em:targetApplication>

<!-- Front End MetaData -->
<em:name>Sample!</em:name>
<em:description>A test extension</em:description>
<em:creator>Your Name Here</em:creator>
<em:homepageURL>http://www.foo.com/</em:homepageURL>
</Description>
</RDF>

Properties and Description

  • sample@foo.net – the ID of the extension. This is some value you come up with to identify your extension in email address format (note that it should not be your email). Make it unique. You could also use a GUID.
  • Specify
    2 — the 2 declares that it is installing an extension. If you were to install a theme it would be 4 (see Install Manifests#type for other type codes).
  • {ec8030f7-c20a-464f-9b0e-13a3a9e97384} – Firefox’s application ID.
  • 1.5 – the minimum version of Firefox you’re saying this extension will work with. Set this as the minimum version you’re going to commit to testing and fixing bugs with.
  • 2.0.0.* – the maximum version of Firefox you’re saying this extension will work with. Set this to be no newer than the newest currently available version! In this case, “2.0.0.*” indicates that the extension works with versions of Firefox 2.0.0.0 through 2.0.0.x.

Extensions designed to work with Firefox 1.5.0.x at the latest should set the maximum version to “1.5.0.*”.
See Install Manifests for a complete listing of the required and optional properties.
Save the file.

Step #6

Create a text file called sample.xul under chrome/content. (e.g: C:\extensions\myExtension\content\sample.xul ). Copy this sample code above and paste it into sample.xul. Then, Save it.

<?xml version="1.0"?>
<overlay id="sample"
   xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<statusbar id="status-bar">
<statusbarpanel id="my-panel"
  label="Hello,World"/>
</statusbar>
</overlay>
 

Step #7

Open the file called chrome.manifest (e.g: C:\extensions\myExtension\chrome.manifest) And Add in this code below:

content     sample    chrome/content/
overlay chrome://browser/content/browser.xul chrome://sample/content/sample.xul

Step #8 – Test

  1. Open your Firefox Profile Folder
    Note: Do you know where your Firefox Profile Folder is?
    If you dont know, Type “%userprofile%” in Run. Or go and find the folder same as login name under Documents and Settings Folder. then uncheck “Show Hidden files and Folder” of Folder Option. (I already told you where Folder Option is. :)) After that, go to Application Data\Mozilla\Firefox\Profiles Folder .
    Yes. This is your Firefox Profile Folder. You will see the folder called “extension”(Sometime, there are some sub folders. eg: kmdxbpyy.default or ujwao9qo.default. Check into them.) I found it under “%userprofile%\Application Data\Mozilla\Firefox\Profiles\kmdxbpyy.default\“..
    More:Profile folder – MozillaZine
  2. Create a folder and give the id of your extension as its name. eg: sample@foo.net
  3. Close Firefox Browser if it’s opened.
  4. Launch Firefox Browser again. You will see “Hello, World” in the Status Bar of Firefox browser.

Note: The way of testing is not same as the original article. You may also find the original way in here.

Updated!

Result ~

Firefox Extension - Sample

frefox-extension-hello-world.jpg

Download ~ http://michaelsync.net/demo/sample@foo.net.zip

That’s all. It’s easy. isn’t it? :)

More Info: Mozilla Firefox > Extensions

MSFirefox or IE7?

Did u know that?

I came to know a web site called MSFirefox today.

logo.PNG

I’m wondering if Microsoft really made that product and this website because there are a lot of things which are very very strange to me.

1. Smiling Bill Gates Photo at the left banner.

smiling-bills.PNG

2. The one they are giving as download is IE7-WindowsXP-x86-enu.exe

This is the installer for Internet Explorer 7.

3. It’s not FREE

30$?

4. The definition of RSS in Full Features Page or this page.

RSS means … Arh! .. Better go and check it out.

If you wanna read more, you may chk their support page.

5. Strange Photo in 4th Feature “Fast Fun” which is mentioned in Home Page.

That girl from this photo looks very like one of the porn stars from Japan. (but not sure.)

Yeah. Maybe there are some more but let me stop here.

I just assume that this website might be created by someone who hate Microsoft. Let me know if you guys have any idea about this site.

Opps! Almost forget.. Here is a movie from their site..

Vote for ME

My friend “Ashish C” and Me are running a competiton to find the best extensions for Firefox 2.

The Rules were:

  1. Each of us will nominate 3 extensions via email.
  2. We will post up both of our nominations on our blog.
  3. Our readers and commentators will vote on the Best extension among our choices.
  4. Voters have until 26th November (12:00 AM GMT) to vote for the best extension.
  5. Winners will be announced on 27th November by counting votes on both blogs.
  6. To make this competition fair we decided not to nominate any of the featured or well known extensions such as AdBlock Plus, Performancing etc.

Ashish C.’s Nomination list:

Translator Translator is web translation extension that allows
you to translate web pages into many of different languages at the click of a button. (The button’s on the lower-right corner of the screen on the status bar.)
   
MediaPlayerConnectivity Allow you to launch embed video of website in an external application with a simple click.
   
Googlepedia Shows you a relevant Wikipedia article along with your search results. Clicking links in the article will trigger new Google searches, making it a very useful research tool.

Note: You may also read from his post.

Michael Sync’s (Me) Nomination List:

DownloadHelper DownloadHelper is a tool for downloading Embeded Video from any website to your harddisk.

More Info:If you wanna know more about this extension, you may read it from my post “Downloading Flash File using DownloadHelper”

   
TestGen4Web TestGen4Web is a tool to record/replay user actions in
Firefox. (This extension is developed by Spikesource, a company making a name for themselves by testing Open Source applications. ) As it acts like Macro-recording in automation testing tools, this extension can be used for testing web application.

More Info: I also wrote about this extension in this post “TestGen4Web, Free Website Testing Tool”.

   
Firebug FireBug lets you explore the far corners of the DOM by keyboard or mouse. All of the tools you need to poke, prod, and monitor your JavaScript, CSS, HTML and Ajax are brought together into one seamless experience, including a debugger, error console, command line, and a variety of fun inspectors.

More Info: You may read about this extension in details from this link “Firebug, Javascript Debugger“.

Okay. That’s all. :-) I have selected three extensions that I think they are the best from a lot of other Firefox Extensions. I also wrote about each extension in details.

So, What do you think? :-) Feel free to let us know your idea. Vote for us!And As Ash says, Let the best man wins!

BTW, When I was looking for the best extensions, I found some cool extensions that I like. I will post about those extensions too during this week.

Thanks and Regards,
Mike