2013-04-23

javascript

Quick JavaScript Debugging Tricks

Java developers generally do their work in a cycle of code, build (compile), execute. The Java Virtual Machine included a HotSwap feature since 2002. Later this functionality was added into the Instrumentation API. This allows for limited update to class bytecode in place (now commonly used in debuggers). JRebel is a commercial third party product that allows for far more extensive reloading of java classes.

Limitations on reloading code in this way simply don't exist in JavaScript. Without going into the details of why this is possible, the following are a few examples of how code can quickly and easily be modified. This results in the possibility of a very different workflow while working in JavaScript. The fact that coding can be done in the browser console itself is significant. A cursory knowledge of JavaScript's capabilities and modern browsers means that JavaScript can be modified and debugged on almost any web page (even those in a production environments) which can allow for quick identification of problems and corresponding solutions.

The following are a few simple techniques available in JavaScript that take advantage of its relatively dynamic nature and allow you to extensively modify the behavior of any web page you like.

Add a Script to a Page

JavaScript bookmarklets are a popular example of the ability to add JavaScript to a page. Some are fun (like fontbomb) but they can also be used for development (for example the jQuery bookmarklet). With a few lines of code in the browser console, you can include a publicly available JavaScript file into a page. Google and others Content Delivery Networks host popular JavaScript libraries so there are plenty readily available.

This example uses Native JavaScript to include jQuery in a page:

	var script= document.createElement('script');

	script.type= 'text/javascript';

	script.src= 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';

	document.head.appendChild(script);	

If jQuery is already available, you can use it and grab a script in a single function call:


	$.getScript('http://underscorejs.org/underscore-min.js');	

If you ran both of these commands in a page that includes some list items (li tags), the following will show both in action - underscorejs's each() function and a jQuery selector.


	_.each($('li'), function(li){console.log(li)});	

Modify an existing page

To change an attribute of a DOM Element: Navigate to http://oreilly.com/ and you can resize the logo like so:


	$('.logo a img').width('400px');	

Similarly, you can change the value of a variable or text:

	$('span.item').first().text('Really Popular Topics:');

There are even ways to override an existing library's functions..

Modifying a JavaScript objects prototype allows allows you to change the behavior of existing objects. If we start with an existing function that modifies the argument passed to it (in this case, trimming leading whitespace).


	function ltrim(str)

	{

	  return str.replace(/^[ \t]+/, '');

	}



	var text = '    This is a Test!';  



	ltrim(text)

Now modify the function so that it will use "this" - the containing object in this context (and remove the parameter). Assign the function to the String prototype. Now all JavaScript objects that have String as their prototype can have ltrim() called on them and will return the String with it's leading whitespace removed.

	function ltrim()

	{

	  return this.replace(/^[ \t]+/, '');

	}



	String.prototype.ltrim = ltrim;

	

	text.trim()	

Some of the very characteristics that make JavaScript difficult (handling of scope, availability of globals) also make it rather easy to manipulate pages wherever they may be. None of the techniques above are introduced because they are particularly valuable when coding a specific application. They are extremely useful when debugging and troubleshooting pages in arbitrary environments, and allow for quick iterations when experimenting with new code.

comments powered by Disqus