当前位置:Gxlcms > 数据库问题 > [html5]使用localStorage兼容低版本Safari无法使用indexeddb的情况

[html5]使用localStorage兼容低版本Safari无法使用indexeddb的情况

时间:2021-07-01 10:21:17 帮助过:35人阅读

|| window.webkitIndexedDB || window.msIndexedDB; window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
    var db = {
        version: 1, // important: only use whole numbers!
        isSupport: function () {// support indexeddb or not
            if (!window.indexedDB)
                return false;
            return true;
        },
        ......
      }

一个例子

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/index-store-db-1.0.js"></script>
    <script>

        var user1 = { id: 1, name: "wolfy", age: 20 };
        var user2 = { id: 2, name: "wolfy", age: 20 };
        //set local indexed db name
        app.db.objectStoreName = "app_test";
        //save data to indexed db
        app.db.save(user1, function () {
            console.log("Save success");
        });
        app.db.save(user2, function () {
            console.log("Save success");
        });
        //query user by id
        app.db.get(1, function (item) {
            console.log("query success", item);
        });
        //query all user
        app.db.getAll(function (items) {
            console.log("query all success", items);
        });
        
        app.db.delete(1, function () {
            console.log("delete success");
        });
    </script>
</head>
<body>

</body>
</html>

结果

技术分享

[html5]使用localStorage兼容低版本Safari无法使用indexeddb的情况

标签:解决办法   技术   god   tidb   number   doc   tran   rip   rename   

人气教程排行