Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.
3rd January 2008 - 6 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.
var count = '';
var numbers = new Array(3);
numbers[0] = 42;
numbers[1] = 13;
numbers[2] = 73;
for(i in numbers){
count += numbers[i]+' ';
}
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.
var html = '<table border="1">';
html += "<tr><th>Navigator Properties</th><th>Value</th></tr>";
for(i in navigator){
html += "<tr><td>" + i + "</td><td>" + navigator[i] + "</td></tr>\n";
}
html += "<tr><th>Window Properties</th><th>Value</th></tr>";
for(i in window){
try{
html += "<tr><td>" + i + "</td><td>" + window[i] + "</td></tr>\n";
}catch(err){
// skip item
}
}
html += "<tr><th>Document Properties</th><th>Value</th></tr>";
for(i in document){
try{
html += "<tr><td>" + i + "</td><td>" + document[i] + "</td></tr>\n";
}catch(err){
// skip item
}
}
html += '</table>';
var div = document.getElementById('test');
div.innerHTML = html;
This produces the output.
Navigator Properties
Value
appCodeName
Mozilla
appName
Netscape
appVersion
5.0 (Windows; en-GB)
language
en-GB
mimeTypes
[object MimeTypeArray]
platform
Win32
oscpu
Windows NT 5.1
vendor
vendorSub
product
Gecko
productSub
20071127
plugins
[object PluginArray]
securityPolicy
userAgent
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11
cookieEnabled
true
onLine
true
javaEnabled
function 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:
[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.
[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.
[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.
[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.
html += "<tr><th>Plugins</th><th>Value</th></tr>";
for(i in navigator.plugins){
html += "<tr><td>" + i + "</td><td>" + navigator.plugins[i] + " " + navigator.plugins[i].name + "</td></tr>\n";
}
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.
html += "<tr><th>Plugins</th><th>Value</th></tr>";
for(i in document.links){
html += "<tr><td>" + i + "</td><td>" + document.links[i] + " " + document.links[i].title + "</td></tr>\n";
}
A pie chart is a great way of showing the relationship between numbers, especially when showing percentages. Drawing a pie chart from scratch takes a fair amount of maths to get working and so people usually go for third party charting libraries in order to add charts to the page.
Nightwatch.js is an end to end testing framework, written in JavaScript. It can be used to test websites and applications and uses the W3C WebDriver API to drive modern browsers to perform the tests.
In this article I will look at setting up Nightwatch.js in a project and getting started with writing tests.
Marp or Markdown Presentation Ecosystem is a collection of tools that allows the generation of presentations of different formats from a markdown template. The tools are written in JavaScript and have a number of options that allow presentations of different styles and formats.
Comments
Submitted by Michael Grier on Tue, 04/14/2009 - 13:53
PermalinkAdd new comment