For Loop Debugging In JavaScript

3rd January 2008 - 5 minutes read time

The for loop in JavaScript can be used to iterate through all items in an array or properties of an object. This makes looping through any object or array very easy. As in the following example that printing out all items in an array.

  1. var count = '';
  2. var numbers = new Array(3);
  3. numbers[0] = 42;
  4. numbers[1] = 13;
  5. numbers[2] = 73;
  6. for(i in numbers){
  7. count += numbers[i]+' ';
  8. }
  9. alert(count);

The three main objects to do with a JavaScript are navigator, window and document. Although window is part of navigator and document is part of window. So to print off all information to do with a browser you can do the following. Note that some items in window and document can cause JavaScript to crash in some browsers so some error detection is included here.

  1. var html = '<table border="1">';
  2. html += "<tr><th>Navigator Properties</th><th>Value</th></tr>";
  3. for(i in navigator){
  4. html += "<tr><td>" + i + "</td><td>" + navigator[i] + "</td></tr>\n";
  5. }
  6. html += "<tr><th>Window Properties</th><th>Value</th></tr>";
  7. for(i in window){
  8. try{
  9. html += "<tr><td>" + i + "</td><td>" + window[i] + "</td></tr>\n";
  10. }catch(err){
  11. // skip item
  12. }
  13. }
  14. html += "<tr><th>Document Properties</th><th>Value</th></tr>";
  15. for(i in document){
  16. try{
  17. html += "<tr><td>" + i + "</td><td>" + document[i] + "</td></tr>\n";
  18. }catch(err){
  19. // skip item
  20. }
  21. }
  22. html += '</table>';
  23. var div = document.getElementById('test');
  24. div.innerHTML = html;

This produces the output.

Navigator PropertiesValue
appVersion5.0 (Windows; en-GB)
mimeTypes[object MimeTypeArray]
oscpuWindows NT 5.1
plugins[object PluginArray]
userAgentMozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv: Gecko/20071127 Firefox/
javaEnabledfunction javaEnabled() { [native code] }

Note : Table cut off for brevity.

Internet Explorer will label everything with the value of that property or as an "object" regardless as to the object type. Firefox, Safari and Opera will give more meaningful output with a number of different object types. A couple of object types to watch out for are as follows:

  1. [native code] : This is native JavaScript code. For example, in the document object you will see a property called write with the value of function write() { [native code] }. This is the document.write function that can be used to write output to the browser.
  2. [object HTMLCollection]: This is a collection (or array) of HTML objects. For example, every link in the document body is kept in the links property.
  3. [object Window]: For the frames property this object is an array of windows, one object for each frame. This object type is called [object WindowCollection] in Opera.
  4. [object PluginArray]: This is an array containing information about the plugins available in the browser which is not available in Internet Explorer. To find plugins for Internet Exploer you will need to use VBScript. Firefox has additional untility property and functions. If you wanted to know is the user had flash installed then you could use this array to look for the Shockwave Flash plugin. Each plugin object has a property of name that you can use to see what the plugin is.

To find the values of any inner object you can use the dot notation in the same array as before. For example, to see all of the plugins available for your browser use the following code (not in IE). As each plugin has a name property this has been included to detail the name of each plugin.

  1. html += "<tr><th>Plugins</th><th>Value</th></tr>";
  2. for(i in navigator.plugins){
  3. html += "<tr><td>" + i + "</td><td>" + navigator.plugins[i] + " " + navigator.plugins[i].name + "</td></tr>\n";
  4. }

To find all of the available links on a page you can use the document.links array. As each link should have a title tag this can be referenced use the title property of each link.

  1. html += "<tr><th>Plugins</th><th>Value</th></tr>";
  2. for(i in document.links){
  3. html += "<tr><td>" + i + "</td><td>" + document.links[i] + " " + document.links[i].title + "</td></tr>\n";
  4. }



If a value cannot be read, at least you can list its key:
  1. try{
  2.   msg += "[:tr:][:td:]" + i + "[:/td:][:td:]" + ((window[ i ]).toString()) + "[:/td:][:/tr:]";
  3. }catch(e){
  4.   msg += "[:tr:][:td:]" + i + "[:/td:][:td:](cannot read value)[:/td:][:/tr:]";
  5. }
since I assume the html characters won't post in here, replace all "[:" with "less than" characters and ":]" with "greater than" characters. ;-)

Michael Grier (Tue, 04/14/2009 - 13:53)

Add new comment

The content of this field is kept private and will not be shown publicly.