Using Browser console for debugging Test Driven JavaScript

One of the great things about TTD is the ability to track the success of each action in the application, whether it was user initiated or an automatic application function. For me, so far, one of the best ways to do this is using the debug console built into the browser. Each browser is specific, of course, but there is a simple technique to use the code cross browser without generating errors.

if(!window.console){ window.console = {log: function(){} }; }

An example of usage is in my form content wizard plugin.

  // look into step for custom button, then add the "begin" button to the first step if not there.
 if(!beginbuttonclass){ 
var beginbuttonclass=""; 
console.log('Note from content display wizard plugin: beginbuttonclass has not been defined. See documentation for more information.');}

To see if an external  script is loaded sucessfully, you need to have a variable that the script would provide, then check for it. For example here we will try see if  I have successfully  loaded JQuery from the cdn: (code.jquery.com/jquery-1.10.2.min.js)

try {
     if($()){console.log("Message from slider admin: Jquery loaded... now executing document load functions...");} 
} catch (e) { console.log("Message from slider admin, error loading jquery..."+e.message); }

 

Leave a Reply