Javascript indexOf – String vs Array

As of Javascript 1.6 indexOf() is a method available for use with Arrays. However, there’s one subtle difference when operating on a String and an Array, both are case sensitive, Array.indexOf() is type sensitive. This is documented but for those of you who’ve not bothered with the documentation, and it’s not working as expected, here’s a heads up on a subtle gotcha…

<script type="text/javascript">
	var list = new Array("1",2,"3",4,"5");

	console.log("Index of 1: " + list.indexOf(1));
	console.log("Index of 2: " + list.indexOf(2));
	console.log("Index of 3: " + list.indexOf(3));
	console.log("Index of 4: " + list.indexOf(4));
	console.log("Index of 5: " + list.indexOf(5));

	var jist = list.join(",");

	console.log("Index of 1: " + jist.indexOf(1));
	console.log("Index of 2: " + jist.indexOf(2));
	console.log("Index of 3: " + jist.indexOf(3));
	console.log("Index of 4: " + jist.indexOf(4));
	console.log("Index of 5: " + jist.indexOf(5));
</script>

Viewing the debug output in Firebug we can see the type checking; the string elements in the array are not found. Concatenate the array to a string and perform the same check – it’s now type insensitive.

Firebug debug output

Javascript sub string nuances

I’m surprised I’ve not run into this before but Javascript has two sub string methods.

[String].substr(start, length);
[String].substring(indexA, indexB);

To clarify by example:

var name = "Greg Knapp";
alert(name.substr(5, 5)); // Knapp
alert(name.substring(5, 10)); // Knapp

Minor but caught me out this afternoon.

Safari 4 bug?

This might not strictly be a bug – more a choice by Apple / Web kit over compliance, anyway…

I noticed this issue in Safari, my primary browser, (Firefox doesn’t exhibit the same problem) while downloading a file from a system I wrote. The system uses content disposition. When you click to download the file, Safari retreives the file and saves it to disk with the correct name. If you right click and ‘Download Linked File As …’, it seems to ignore the suggested filename and uses the end part of the URL, excluding the query string (everything after and including ‘?…’).

This is not the first time Safari has stumbled. An internal indexing system at work uses prototype to submit forms (so we can chain event handlers on submit). The forms themselves (maybe incorrectly?) do not specify action or onSubmit attributes. Safari can’t submit these forms, Firefox doesn’t have a problem and works. </grumble>

Blog upgraded

Well that was easier than I expected, having not upgraded my blog in years, I figured it was long overdue and am lucky not to have been defaced via vulnerability exploits. Moving from Wordpress 2.0.2 to 2.8.1 was a simple backup and upload of the latest source files. Hurrah!

Configuring Canon PIXMA MP620 wirelessly on Mac OS X

Update April, 2010: I’m happy to report this pain is avoided with the latest (10.26.2) MP620 CUPS drivers from Canon when installing this printer on Snow Leopard. After installing the drivers, then adding a new printer, you have to wait a few minutes for SL to detect the printer before it displays it in the list.


This might seem like dumb thing to blog about but I spent an hour or more trying to get this to work. Partly due to my own stubbornness, I resent using driver installation CDs. The driver on them is typically out of date (certainly for graphics cards), and they’re loaded with bloatware crap you don’t need to operate your device / peripheral. Manufacturers love to re-invent the wheel, providing a utility for a service that already exists within the operating system.

So I ran off to the Canon site and grabbed the latest MP620 CUPS driver, thinking this would be a simple install and “Print Test Page”, job done – but nooooooo.

So the bad news, yes you have to insert the Macintosh setup CD. I selected “Custom Install” and selected the driver, the MP Navigator EX software (for scanning) and the Canon IJ Network utility. Despite only wanting to install this printer over a wireless network connection, you must elect to install it via USB first.

Once set up is complete, you’ll have to restart and then be presented with the connection type you’d like to use to communicate with your MP620. Select USB.

At this point it’s probably wise to check your firewall is disabled in OS X (just temporarily), head to System Preferences, click the ‘Security’ icon, select the Firewall tab, and check ‘Allow all incoming connections’ (this turns your firewall off, and potentially lets the bad guys in). This way we won’t run into problems using wireless later.

1. Click ‘Show All’ then ‘Print & Fax’, you should see ‘Canon MP620 series’ in the list.

2. Select your MP620 then click the ‘Open Print Queue…’ button.
3. Click ‘Utility’ on the right hand side of the toolbar.

4. In the window that opens, select ‘Network’ if it’s not already selected.
You should see your MP620 in the list, select it.
5. Click the ‘Network Settings’ button, you should now be able to set your wireless access point, and enter the passphrase.
6. Once you’ve done this, add this printer.
You’ll have two MP620’s but one of them will have a MAC address (e.g. 00:00:85.af.62.f0) in the title or settings, this is the one you want to keep, remove the other printer profile which is the USB connection.
7. You can remove the profile with the ‘-’ button.
8. Unplug your USB cable.
9. On the wireless profile, click the ‘Open Print Queue…’ button and from the Printer menu select ‘Print Test Page’.

Voila! You’re printing wirelessly.