Ways to debug your jQuery or JavaScript codes

Debugging your client code is rather a normal procedures for any web developers. Everyone will shout Firebug! yeah, me too. But Firebug is great for syntax detection but how about logic problem? In this article i will share with you some of the ways i used to debug my JavaScript or jQuery codes when I'm developing my web application. I will also share with you a trick that i used on my code to alert me that a bug occurs in a particular script since i don't get many helpful users nowadays.

Alert Them Out

The most simple and basic way of debugging is by using JavaScript alert function. This is old but is quite useful sometimes. Especially when you do not want to remember other debugging methods. It's pretty simple, alert the message you want to see. If the alert doesn't appear, this means that the error is before the alert statement. I usually do this when I'm debugging IE although there are tools available for IE.

alert("The Bug Is Somewhere Before ME!")

Log them up

Well, if you are using WebKit browsers for your development (FireFox, Chrome, Safari) you may want to try this.

if (window.console)
  window.console.log("The Bug is killing me");

What this does is to log the string 'The Bug is killing me' into the console such as Firebug. It's always better than using alert and see this infinity loop that keep popping out until you are force to shut down your browser!

Log them with jQuery

The above two methods work both in jQuery and JavaScript. But this only function well in jQuery. This is definitely not something i came up with but its from Dominic Mitchell

  jQuery.fn.log = function (msg) {
      console.log("%s: %o", msg, this);
      return this;
  };

The above creates a function, 'log'. This function will do exactly similar to the one above but the differences is that it format the string a little bit before displaying out to the console. This is good for debugging your long chaining with jQuery. Although the previous method also can be used to debug chaining but it will required an additional line instead of including it into the chaining process. So you can debug this way,

  $(root).find('li.source > input:checkbox').log("sources to uncheck").removeAttr("checked");

Try and catch

In JavaScript, you can try to catch the error in a particular scope. This way, it won't propagate to other section of the code.

try
  {
  //Run some code here
  }
catch(err)
  {
  //Handle errors here
  }

This is pretty good when you are trying to debug one of the many function in your JavaScript.

Catch them all

The last one i am going to show you is to catch any one of the error into a particular function instead of using multiple try and catch.

function handleError(e)
{
alert(’An error has occurred!\n’+e);
return true;
}
window.onerror = handleError;

This will handle any error occurs in your JavaScript code to the function 'handleError'. You will want to use this at the end of your script. Especially if you want to be informed whether a particular function or script has malfunction and the users are not reporting. Basically, what you can do is to gather all information and placed them into a String or JSON. Using ajax to delivery these information to your email or database. This way, your support team will have an idea which part are having problems and your system will be more reliable. (testing doesn't means 100% bug free) The information that you may want to know are usually located at Navigator Object in JavaScript.

Summary

These are the methods i usually look at when debugging my own client script. It might not be everything but i hope it can be useful to some of you out there. Hope you learn something!

Safari plugin for your web development need

Safari has been Mac user favourite browser when it comes to surfing the world wide web.  However, web development using Safari may still seems a bit troublesome compare to Mozilla Firefox. But things is starting to change. Safari users are starting to develop their own unique Safari plugin to share with other developers around the world. Web development on Safari may change slowly when more Safari plugin is being developed, here are some of Safari plugin that developers used on web development.

Safari Tidy

safari-tidy

Safari Tidy is plugin that lets you validate the webpages you browse for (x)html compliance. It is a validator that will show three status on Safari, warning, error, ok in a small icon on the lower right of the browser. If there is any error on your HTML, Safari Tidy will detect it!

Drosera

drosera

Drosera is a JavaScript debugger for WebKit that can be used with any application that uses WebKit. Like the Web Inspector, over 90% of it is written in HTML, CSS and JavaScript. If you are using a lower version of Safari, this is something you need for debugging as this has been replaced by Web Inspector.

Web Inspector

web-inspector

This is something alike Firebug in Mozilla Firefox. The Web Inspector lets you browse the live DOM hierarchy in a compact HUD style window. It also highlight the node on the page as it is selected in the hierarchy. You can also search for nodes by node name, id and CSS class name. This is a must have plugin if you are doing web development with Safari.

XRAY

xray

XRAY is not really a Safari Plugin. It is a bookmarklet for most browser that can be used to look at the box model for any element on the page. You will just have to drag it onto your bookmark panel and hit on XRAY to see the details of the particular box or div. Details such as height,weight,position, etc. Mostly CSS rule details as shown below.This is very handy.

xrayexample

Safari Source

safari-source

This simple plugin allows you to adds syntax coloring to Safari's source view so that it can be view much easier.

Safari Stand

safari-stand

An InputManager plugin that adds various useful functionality, such as bookmark/history searching, thumbnail tab sidebar, view related cookies/css/images for a site. This can be used with Safari Tidy side by side.

Saft

salt

Saft is more like a improvement plugin for Safari than a web development plugin. Nonetheless, it can help you to speed up web development such as crush protection.

If the above Safari Plugin cannot serve you on your web development in Safari, you may wish to look for web based solution to continue web development. The other solution is to use cross browser plugin such as Firebug Lite, Fiddler, NitobiBug or DebugBar which can be found in Here. That is all i have for Safari plugin for your web development need. If you have any more cool plugin you would like to share. Please share it with us!

Useful Internet Explorer Extension For Your Web Development

There are a rare number of Internet Explorer developer extension available on the net. Furthermore, most of them required payment which really discourage poor programmers like me. I have kept a few free Internet Explorer extension of my own and I believe this should be kept in an article and share with everyone else.

Web Accessibility Toolbar

vision-aus

Web accessibility toolbar is an extension for Internet Explorer and Opera developers. The Web Accessibility Toolbar has been developed to aid manual examination of web pages for a variety of aspects of accessibility. It provides some of the function such as resizing, validating, doc info and etc. it is one of the freeware available for Internet Explorer.

DebugBar

debugbar

This is the most advance debugging tool for Internet Explorer. Firefox has Firebug and this is what IE has.  It has the following features,

  • DOM Inspector: View DOM Tree and modify tags attributes and css attributes on the fly to test your page
  • HTTP Inspector: View HTTP/S request to check cookies, GET and POST parameters, view server info
  • Javascript Inspector and Javascript Console: View javascript functions for easier debugging, see Javascript and AJAX code
  • HTML Validator: Validate HTML code to correct and optimize your code and html size of your page
  • And many more features: See page cookies, get pixel color on a page, make a page screenshot...

It is free for both personal and education use but commercial will have to acquire a license for it.

Internet Explorer Developer Toolbar

iedevelopertools

Crap. I understand this is a ALL browser website tools but this one i just can't let it go anywhere else! Firefox has Firebug and webdeveloper to fire out all the bug in Mozilla, IE has developer toolbar! This toolbar is created by Microsoft and gives most of the ability of both Firebug and webdeveloper but not all of it. This is something you should download and install for your web development or debugging for IE.

NitobiBug

nitobibug

One of the great crossbrower javascript debugging tool. NitobiBug is JavaScript object logger and inspection tool similar to Firebug but unlike Firebug, this support many other browser which you can use for your debugging with ajax and javascript. This is one of the best debugging tool available for other browers.

Fidder

fiddler

Fiddler is a Web Debugging Proxy which logs all HTTP(S) traffic between your computer and the Internet. Fiddler allows you to inspect all HTTP(S) traffic, set breakpoints, and "fiddle" with incoming or outgoing data. Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language.

Web Development Helper

web-development-helper

Web Development Helper is a free browser extension for Internet Explorer that provides a set of tools and utilities for the Web developer, esp. Ajax and ASP.NET developers. The tool provides features such as a DOM inspector, an HTTP tracing tool, and script diagnostics and immediate window. Web Development Helper works against IE6+, and requires the .NET Framework 2.0 or greater to be installed on the machine.

Update: 23/4/2009

Firebug Lite

firebug

A excellent extension for Internet Explorer, Firebug Lite. This extension simulate some Firebug function in Mozilla Firefox for greater debugging possibility in Internet Explorer! Credit goes to Rich for being this up!

If the above doesn't really provides what you need for internet Explorer debugging especially in CSS, you may want to use web based tool when debugging in Internet Explorer. That is all i have from my side. What other Internet Explorer tools you have to debug your CSS and JavaScript?  Share with me!

List Of Must Have Addons For Programmers In Web Development

There are many tools developers used during web development and definitely some of these tools i mention here won't be any new to some of the front end developers who has been developing for many years. However, this is the set of tools i used and maintained overtime so that any changes made on my computer, i will still be able to track back all these amazing tools and for some of the newer programmers, this will definitely benefit them. here are a list of fine made tools you can use to help you find those nasty bugs and speed up your own web development.

Firebug

firebug_01

Every developers  love Mozilla Firefox for the particular reason that Firebug is there to assist you on your aJax and JavaScript needs. It automatically detect your errors and show a nice error bar below your browser just like this.

error-firebug1

Firebug can be use to optimize your codes as it provides you with all the load time of each individual script on the web and many other useful information you used on your script. Firebug also contains a feature that allow you to inspect element changes on the HTML while your code is manipulating the DOM. There are just so many benefits in this application that its a MUST have add-on if you are using Mozilla Firefox for your development.

IE Tab

ietab

If you think IE View is not a bad application for testing your site on IE using Mozilla Firefox, you should try IE tab then. It allows you to open a IE browser instead! AWesome! Window update anyone?

Html Validator

htmlvalidate

Help you debug your HTML structure to check for any error on your HTML. It provides a nice icon to indicate whether your HTML codes are properly written. Furthermore, it also helps validate other pages that you send in via ajax!  This is great for WC3 Validation tools! The addons errors indication are shown below

  • errors: HTML errors that Tidy cannot fix or understand.
  • warnings: HTML errors that Tidy can fix automatically
  • (optional) accessibility warnings: HTML warnings for the 3 priority levels defined in W3c WAI

Add N Edit Cookies

addnedit

If you deal with cookies  a lot, this is something you must have! It gives you the ability to edit and add cookies on the fly! Especially when you are dealing with server sided scripting or log in page.

FireFTP

fireftp

This is one of the important addon you should have if you are dealing with server scripting. Imagine clicking left and right between your browser and ftp client and pile up all the different application during your web development. This help development much faster and cleaner.

YSlow

yslow

Ever wonder whether your site is optimized? developed by Yahoo group, this addon tells you your website problems and why it is slow! It grade your website and tell you where to improve so that you won't be lost guessing where to improve on your site to better enhance its peformance. Great stuff for your optimization need.

Live HTTP Headers

livehttpheader

another great tool a developer must have. Live HTTP Headers let you view information on the http header LIVE checking out what kind of cookies is being sent to your site, edit request header and even reply an URL! This application usage is as follow

  • Help debugging web application.
  • See which kind of web server the remote site is using.
  • See the cookies sent by remote site.

FirePHP

firephp

PHP user must see application! Especially for people who deal with ajax and PHP, this will be useful for you. This will stop you wondering what the heck is happening on your server side when calling on ajax. This small addon of firebug keep a log on firebug console and tell you what is happening on PHP. But this required firebug to be installed and other required too. Nonetheless, this is quite useful. (read up the requirement by clicking on the image of FirePHP above to read it on their development site)

Session Manager

sessionmanager

Session Manager saves and restores the state of all windows - either when you want it or automatically at start up and after crashes. Additionally it offers you to reopen (accidentally) closed windows and tabs. If you're afraid of losing data while browsing - this addon allows you to relax. This is most appropriate when dealing with log in web portal and your script just crush up your browser and required you to login again. Speed up your development.

Total Validator

total validator
total validator

Performs accessibility validation (WCAG, US-508), broken link checking, spell checking (5 languages), and takes screen shots using 32 browsers on Windows, Linux, and OS/X. Like it? i do =D

Console

console

Update on default console on Mozilla Firefox. It provides a better overall error details compare to the default ones on Firefox. Web development must have upgrade.

XML Developer Toolbar

xmldeveloper

if you deal with a lot of XML, this is an addon that you might want to have. It just speed up the overall procedure of you dealing with XML.

Library Detector

library detector
library detector

finally, if you haven't had any chance of dealing with framework library such as dojo, jquery,mootools, etc. i think you should start digging your resources. This addon gives you the information on what library the current view website is using. Quite useful for digging technology!

User Agent Switcher (23/04/2009)

user-agent

User Agent Switcher is REQUIRED in web development. Especially when your script or deign is catering for different browser. This will definitely help you tons during web development. This means no more switching browser to test your script for cross browser compatibility.

PageDiff (23/04/2009)

pagediff

What pageDiff does is to compare your pages during web development. You will find this pretty useful when dealing with DOM manipulation.  You can save the page and edit all you want then compare with the version you have previously saved. Pretty useful i must say.

CodeBurner (25/4/2009)

codeburner

Another Addon for Firebug to make it more powerful. This extension allows you to do a lookup on HTML and CSS element in Firebug! Furthermore, it gives you detailed list of the HTML tags, attributes and CSS that are being used by the element you are inspecting, including browser support. It comes very handy when you don't remember what worked in what browser or if you need to refresh your memory about an unusual tag or CSS rule. Including links to get further information about a particular element, attribute or style is welcomed too together with the search feature. Great stuff!

FireCookie (25/04/2009)

firecookie

Another extension for firebug (who need other extension when everyone is aiming at firebug) . It allows you to manage to view cookies using Firebug. Similar to Add n Edit cookie just that this is for Firebug.

JavaScript Deobfuscator (25/4/2009)

deobfuscator

Pissed when you can't view the code being obfuscated by the web master? Try this then! This deobfuscator the code and allow you to view it normally but only activate this when you actually need it as it takes up quite a lot of resources.

FireCookie (19/09/2009)

ubiquity

A very powerful search plugin that allows you to seek for API within second.

Notice i did not provides some of the very famous color detection etc. addon from Firefox. Those will be on other section not under developers. I will place it on web master and designer instead.  So that i can maintained all the great plugin i have been currently utilised. The web designer ones is up at List Of Must Have Addons For Designers In Web Development

IE, Opera and Netscape issues on ‘Stop page loading’ with javascript

In case you don't know what i am referring on the topic, i really mean to stop the page from loading on the background of the browser. Usually, pressing STOP icon in the browser will stop any activity in a browser. Background activity that we are interested are uploading, preloading, retrieving and etc. of any dynamic activity that the browsers are dealing with when the page fire up ajax functionality. There are only three browser that are in my concern list because these are the browsers that are being used today.

Netscape

Netscape browsers are Mozilla Firefox, Google Chrome and Safari. These are Netscape browser which most developers or Mac users used when developing web application and design. Obviously, finding a solution for Netscape page load function is pretty easy and we can also manually press the X button on the top of the browser to stop all activity in a netscape browser. Ultimately, we will know that there is a function in Netscape browser that can stop all activity since the stop button on the browser is performing such action.  In case you are still wondering what the heck is the javascript code,

window.stop();

The above will stop all activity which include stopping the page from loading and preventing any uploading to occurs. Any preload of image will also be stop by this javascript method.

Internet Explorer (IE)

IE having the majority users in the world is still lagging such function. It is possible to stop page from loading by using the following javascript code.

document.execCommand('Stop')

which will return you an empty page when executed during page load. However, this will only stop the page from loading! Other activity such as uploading and preloading of images will still occur. There is practically no way to stop the background activity in an IE browser. If you fire up an ajax application in an IE browser, you won't see any 'activity' at the bottom of the page indicating it is retrieving the request. Pressing stop button won't disable IE from retrieving unless you escape the page which a dynamic web site do not want it. In short, once a request is fired to IE, there is no cancellation for that request.

Opera

Opera work quitte similiar with IE.  Although by interface and efficiency, Opera is much MUCH superior than IE, it still doesn't gain much market share in the browser industry. So how do we stop activity in Opera? There are definitely some way of stopping the activity in Opera which i personally believe. However, due to the small community available for Opera, you won't find any good answer avaliable in Google.  However, Opera can use event bubbling to stop certain event from happening. (this mean it cannot stop event that has already happen)

Summary

The reason for this article is to prevent people like ME who hunt for such solution so that my RIEG jQuery plugin test site can work perfectly in all browser. And the reason why people will want to stop activity in the background of the browser is because in a dynamic environment, we will want to remove the existing DOM element and replace these DOM element with a new one. However, as existing request is still being sent through the browser, it is necessary to stop those existing request before sending out the new request so that the display will be flawless! Unfortunately, this cannot be done. (sad)