indexeddb-persists.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <html>
  2. <body>
  3. <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>
  4. <p>Please follow these steps in order:</p>
  5. <p>First, click <a href="javascript: setData()">here</a> to open a database and set some data within it.</p>
  6. <p>Next, close the browser and then re-open this page.</p>
  7. <p>Lastly, click <a href="javascript: verifyData()">here</a> to verify the data was there</p>
  8. <p>Status: <span id=status>...</span></p>
  9. <script>
  10. if (!('indexedDB' in window))
  11. 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>";
  12. else
  13. document.getElementById("enabled").innerHTML = "<font color=green>Your build seems to have it enabled.</font>";
  14. function status(str, color)
  15. {
  16. if (color)
  17. str = "<font color='" + color + "'>" + str + "</font>";
  18. document.getElementById("status").innerHTML = str;
  19. }
  20. function setData()
  21. {
  22. status("Something must have gone wrong (or we're still working)...", "red");
  23. indexedDB.open("someDB", "some description").onsuccess = function() {
  24. event.result.setVersion("some version").onsuccess = function() {
  25. var db = event.source;
  26. while (db.objectStoreNames.length)
  27. db.removeObjectStore(db.objectStoreNames[0]);
  28. db.createObjectStore("test").put("value", "key").onsuccess = function() {
  29. status("Value set", "green");
  30. }
  31. }
  32. }
  33. }
  34. function verifyData()
  35. {
  36. status("Something must have gone wrong (or we're still working)...", "red");
  37. indexedDB.open("someDB", "some description").onsuccess = function() {
  38. try {
  39. var result = event.result.transaction([]).objectStore("test").get("key");
  40. result.onsuccess = function() {
  41. if (event.result == "value")
  42. status("Value verified", "green");
  43. else
  44. status("Value incorrect!", "red");
  45. }
  46. result.onerror = function() {
  47. status("An error occurred: " + event.code + " " + event.message, "red");
  48. }
  49. } catch (e) {
  50. status("An exception occurred: " + e, "red");
  51. }
  52. }
  53. }
  54. </script>
  55. </body>
  56. </html>