admin管理员组

文章数量:1330593

I would like to develop an application which uses a browser to interact with the user. The application is to be available offline and distributable via a zip.

The functions I would like to perform are to be handled by HTML, CSS, JS and I would like to make use of the IndexedDB functionality.

I have hit a problem with IndexedDB (Chrome) in that the same code works in an online space but not from a local hard drive location (file://).

Refer to example: /

(function() {

    var db;
    var dbreq = indexedDB.open("TestApp", 2);

    dbreq.onsuccess = function(e) {
        alert("Database created");
        db = e.target.result;

        var employeeStore = db.createObjectStore (
            "employees",
            {keyPath: "id"}
        );

    };

    dbreq.onerror = function(e) {
        alert("Database Error: " + e.target.errorCode);
    };

    dbreq.onupgradeneeded = function(e) {
        alert("Database upgrade needed");
    };

})();

Any suggestions?

I would like to develop an application which uses a browser to interact with the user. The application is to be available offline and distributable via a zip.

The functions I would like to perform are to be handled by HTML, CSS, JS and I would like to make use of the IndexedDB functionality.

I have hit a problem with IndexedDB (Chrome) in that the same code works in an online space but not from a local hard drive location (file://).

Refer to example: http://jsfiddle/FwuUD/

(function() {

    var db;
    var dbreq = indexedDB.open("TestApp", 2);

    dbreq.onsuccess = function(e) {
        alert("Database created");
        db = e.target.result;

        var employeeStore = db.createObjectStore (
            "employees",
            {keyPath: "id"}
        );

    };

    dbreq.onerror = function(e) {
        alert("Database Error: " + e.target.errorCode);
    };

    dbreq.onupgradeneeded = function(e) {
        alert("Database upgrade needed");
    };

})();

Any suggestions?

Share Improve this question asked Mar 29, 2013 at 0:08 Adam RybakAdam Rybak 531 silver badge4 bronze badges 2
  • This works okay for me in both cases, but I had to move the createObjectStore to onupgradeneeded – Explosion Pills Commented Mar 29, 2013 at 4:16
  • If you have python: "python -m http.server" starts a web server on port 8000 in the local directory. – 79E09796 Commented Aug 8, 2013 at 16:11
Add a ment  | 

3 Answers 3

Reset to default 3

In case anyone is looking for an updated answer to this 6-year-old question, both Chrome and Safari allow local HTML files to access IndexedDB API now, without the need for a local server. IE continues to hold out on this it seems. Unsure about Edge or Firefox.

The indexedDB API only works inside a webserver. When you navigate to it using the file system it won't work. The indexedDB API needs a domain context to work in and the file system doesn't provide that. Short you need an url to use the api.

IndexedDB is disabled when run from file:/// for security. It's unclear if you control the browser parameters. If you do, you can pass --allow-file-access-from-files which will allow IndexedDB to work from the file:/// origin.

本文标签: javascriptIndexedDB over local HTML fileStack Overflow