W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2012

Re: [IndexedDB] Problems unprefixing IndexedDB

From: Tab Atkins Jr. <jackalmage@gmail.com>
Date: Wed, 8 Aug 2012 17:12:33 -0700
Message-ID: <CAAWBYDCsxFLZvySe-ZLzzu7Fc2fPBOSrtuPsUOrLKiSqG_TiEw@mail.gmail.com>
To: Kyle Huey <me@kylehuey.com>
Cc: Web Applications Working Group WG <public-webapps@w3.org>
On Wed, Aug 8, 2012 at 5:06 PM, Kyle Huey <me@kylehuey.com> wrote:
> Hello all,
>
> Jonas mentioned earlier on this list that we unprefixed IndexedDB in Firefox
> nightlies some time ago.  We ran into a bit of a problem.[0]  Most of the
> IndexedDB tutorials (including ours and HTML5 Rocks[1] :-/) tell authors to
> deal with prefixing with:
>
> var indexedDB = window.indexedDB || window.webkitIndexedDB ||
> window.mozIndexedDB || window.msIndexedDB || ...
>
> This code has a bug when executed at global scope.  Because the properties
> are on the prototype chain of the global object, 'var indexedDB' creates a
> new property on the global.  Then window.indexedDB finds the new property
> (which has the value undefined) instead of the IDBFactory on the prototype
> chain.  The result is that all of the pages that do this no longer work
> after we unprefix IndexedDB.

Just for reference, the correct way to do this is:

window.indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB || window.msIndexedDB || ...

This avoids the var hoisting that's causing the problems.

~TJ
Received on Thursday, 9 August 2012 00:13:21 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:54 GMT