Html程序  |  70行  |  2.3 KB

<html>
<body>
<p>This is a test that only applies to IndexedDB.  <span id=enabled>Our test for whether you have it enabled seems to have failed.</span></p>

<p>Please follow these steps in order:</p>

<p>First, click <a href="javascript: setData()">here</a> to open a database and set some data within it.</p>

<p>Next, close the browser and then re-open this page.</p>

<p>Lastly, click <a href="javascript: verifyData()">here</a> to verify the data was there</p>

<p>Status: <span id=status>...</span></p>

<script>

if (!('webkitIndexedDB' in window))
    document.getElementById("enabled").innerHTML = "<font color=red>Your build does NOT seem to have it enabled.  So all code on this page is disabled.</font>";
else
    document.getElementById("enabled").innerHTML = "<font color=green>Your build seems to have it enabled.</font>";

function status(str, color)
{
    if (color)
        str = "<font color='" + color + "'>" + str + "</font>";
    document.getElementById("status").innerHTML = str;
}

function setData()
{
    status("Something must have gone wrong (or we're still working)...", "red");

    webkitIndexedDB.open("someDB", "some description").onsuccess = function() {
        event.result.setVersion("some version").onsuccess = function() {
            var db = event.source;
            while (db.objectStoreNames.length)
                db.removeObjectStore(db.objectStoreNames[0]);
            db.createObjectStore("test").put("value", "key").onsuccess = function() {
                status("Value set", "green");
            }
        }
    }
}

function verifyData()
{
    status("Something must have gone wrong (or we're still working)...", "red");

    webkitIndexedDB.open("someDB", "some description").onsuccess = function() {
        try {
            var result = event.result.transaction([]).objectStore("test").get("key");
            result.onsuccess = function() {
                if (event.result == "value")
                    status("Value verified", "green");
                else
                    status("Value incorrect!", "red");
            }
            result.onerror = function() {
                status("An error occurred: " + event.code + " " + event.message, "red");
            }
        } catch (e) {
            status("An exception occurred: " + e, "red");
        }
    }
}

</script>
</body>
</html>