How to get indexedDB to work in IE11?

对着背影说爱祢 提交于 2019-12-12 05:17:18

问题


I want to use indexedDB in my IE11, but it seems to be undefined. This is the code:

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>



    <script>
        window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;

        window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
        window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange

        if (IDBTransaction) {
            window.IDBTransaction.READ_WRITE = window.IDBTransaction.READ_WRITE || 'readwrite';
            window.IDBTransaction.READ_ONLY = window.IDBTransaction.READ_ONLY || 'readonly';
        }
        if (!window.indexedDB) {
            window.alert("Your browser doesn't support a stable version of IndexedDB.")
        }
        alert(document.documentMode);
        alert(document.compatMode);
    </script>
</head>

<body>
 The content of the document......
</body>

</html> 

it alerts:

Your browser doesn't support a stable version of IndexedDB.
11
CSS1Compat

Does anyone know what's wrong?

Thanks


回答1:


I suspect you're trying to do this from a local file (e.g. c:\test.html) rather than an HTTP resource. IE probably restricts access to the API from file (or non-HTTP) origins.

If I save your content locally to a file it alerts as you noted in IE. If I serve the content via a server, it works correctly.

A simpler example:

<script>
alert(window.indexedDB);
</script>
  • Local file: undefined
  • Served: [object IDBFactory]

For example:

alert(window.indexedDB);


来源:https://stackoverflow.com/questions/35510035/how-to-get-indexeddb-to-work-in-ie11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!