Simple clear advice in plain English

Hand on: Inside Google Gears

Take your web applications offline with Google’s open-source browser add-in

Get started with Google Gears
The starting point for a Gears application is a Google-supplied Javascript file called gears_init.js, which sets up a factory object from which you create other Gears objects.

When your application starts, such as in the body.onload event handler, you can check whether Gears is installed and direct the user to the Gears install page if it is not.

I created a simple example, based on the code in Google’s tutorials. The startup function checks for Gears, and if present it creates a managed store and opens a SQLite database. Not much code is needed.

Here are the key lines:

//create a local server object

localServer = google.gears.factory.create(“beta.localserver”,”1.0”);

//create a managed store

store = localServer.createManagedStore(STORE_NAME);

//create a database

db = google.gears.factory.create(‘beta.database’, ‘1.0’);

//create or open a database table

if (db) {

db.open(‘pcw_gears’);

db.execute(‘create table if not exists pcw (somefield varchar(255))’);

}

A managed store is the starting point for caching content for offline use. You populate the managed store by creating a manifest identifying the files you need to cache. The manifest also has a version string. Then you can populate the store like this:

store.manifestUrl = pcw_manifest.json;

store.checkForUpdate();

At a later date the files on the server may change. In order to refresh the cached files, you update the version in the manifest. Next time the code calls checkForUpdate(), the new files will be downloaded.

Working with the database is equally straightforward. Here’s how to write a value to the database:

var inputEl = document.getElementById(‘dbInput’);

var inputval = inputEl.value;

db.execute(‘insert into pcw values (?)’, [inputval]);

and here is how you might get a value back:

var rs = db.execute(‘select somefield from pcw’);

var theValue;

if (rs.isValidRow())

{

theValue = rs.field(0);

textOut(“Database query successful”);

}

This example gets a single value, but the recordset object may contain many rows that you can step through. The advantage of SQLite is its support for standard SQL, along with fast performance.

These may be simple examples, but Gears is a revolution, giving web developers an infrastructure for offline web applications for the first time.

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Using Wizard to create a form

Create a good Base for your data

Databases may sound very dreary but they are behind almost everything we do. We explain how to go about starting one of your own using Libre Office Base

Find a person's location with Google Maps

How to add geographic information to your websites using Google Maps and Javascript

Visual Programming - Bugging out

It's easy to catch program bugs and streamline your code in VisualBasic, says Tim Anderson.

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Restore point

A Windows backup of system files and settings.

Great shopping deals from Computeractive