Ivor O’Connor

April 4, 2009

Stripped Google Gears Causing Accidents

Filed under: debugging, Firefox, GEARS, GOOGLE, howto, JavaScript, Linux, tutorial, typeof, ubuntu — Tags: , , , , , — ioconnor @ 12:02 am

Attempting to use Google Gears when it is not installed on a user’s browser causes an error. You’d think their library would handle this more gracefully than just crashing but it doesn’t.
UPDATED 2009.04.04: Their library should define the call but return something akin to an alert warning the user/developer

The crash occurs on this line:

g_db = google.gears.factory.create(‘beta.database’);

I pulled out my “JavaScript The Definitive Guide 5th Edition” looking for how to get around this. (This is my primary reference book for all things JavaScript. Perhaps if I were better at JavaScript it would be second nature but I’m not good at JavaScript so I spend lots of time looking things up.) I figured “google…” was not defined. So I looked at the index and put a paperclip on each page in the book referenced by the index. Then I tried all sorts of combinations none of which would work. Things like:

if (google === undefined) {
alert(“A”);
} else {
alert(“B”);
}

and

if (google == undefined) {
alert(“A”);
} else {
alert(“B”);
}

and

if (“gears” in google) {
alert(“A”);
} else {
alert(“B”);
}

and

if (google) {
alert(“A”);
} else {
alert(“B”);
}

and

if (google.gears) {
alert(“A”);
} else {
alert(“B”);
}

and

if (“length” in google) {
alert(“A”);
} else {
alert(“B”);
}

but none of above worked! There were no other solutions in my reference book. My reference book doth sucketh.

So I used a try/catch for a while but I’d still get an error message in the “error console”. The try/catch is ugly but it let me do what I needed and polluting the error console seems to be acceptable. Everybody does it. Just leave the error console open while browsing the web to see how rampant the pollution is.

However I revisited this problem today. After extensive googling seeing all sorts of “solutions” that were similar to the above and which did not work I happened to find the following:

if (typeof(google) == ‘undefined’) {

I’m using Firefox 3.0.8 if this makes any difference. It’s the very latest version as of this month from Mozilla. I’ve got to remember this solution in the future!

Advertisements

Blog at WordPress.com.