Advertisement
dav4530

HTML 5 Websql in Firefox with Websql Polyfill

Jan 23rd, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.66 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title></title>
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6.         <script src="wsp.js"></script>
  7.         <script type="text/javascript">
  8.           var html5rocks = {};
  9.           html5rocks.webdb = {};
  10.          
  11.           html5rocks.webdb.db = null;
  12.  
  13.           //opening a database
  14.           html5rocks.webdb.open = function() {
  15.             var dbSize = 5 * 1024 * 1024; // 5MB
  16.             html5rocks.webdb.db = openDatabase("Todo", "1", "Todo manager", dbSize);
  17.           }
  18.  
  19.           html5rocks.webdb.onError = function(tx, e) {
  20.             alert("There has been an error: " + e.message);
  21.           }
  22.  
  23.           html5rocks.webdb.onSuccess = function(tx, r) {
  24.             // re-render the data.
  25.             // loadTodoItems is defined in Step 4a
  26.             console.log('database has been created');
  27.             html5rocks.webdb.getAllTodoItems(loadTodoItems);
  28.           }
  29.          
  30.           //creating a table
  31.           html5rocks.webdb.createTable = function() {
  32.             var db = html5rocks.webdb.db;
  33.             db.transaction(function(tx) {
  34.               tx.executeSql("CREATE TABLE IF NOT EXISTS " +
  35.                     "todo(ID INTEGER PRIMARY KEY ASC, todo TEXT, added_on DATETIME)", [],
  36.                     function(tx, r){
  37.                       console.log('Create table success');
  38.                     },
  39.                     function(tx, e){
  40.                       console.log('Create table error '+ e.message);
  41.                     });
  42.             });
  43.           }
  44.          
  45.           //adding data to a table
  46.           html5rocks.webdb.addTodo = function(todoText) {
  47.           var db = html5rocks.webdb.db;
  48.           db.transaction(function(tx){
  49.             var addedOn = new Date();
  50.             tx.executeSql("INSERT INTO todo(todo, added_on) VALUES (?,?)",
  51.             [todoText, addedOn],
  52.                     function(tx, r){
  53.                       console.log('Insert table success');
  54.                     },
  55.                     function(tx, e){
  56.                       console.log('Insert table error '+ e.message);
  57.                     });
  58.           });
  59.         }
  60.        
  61.         //selecting data from the table
  62.         html5rocks.webdb.getAllTodoItems = function(renderFunc) {
  63.           var db = html5rocks.webdb.db;
  64.           db.transaction(function(tx) {
  65.             tx.executeSql("SELECT * FROM todo", [], renderFunc,
  66.             html5rocks.webdb.onError);
  67.           });
  68.         }
  69.        
  70.         //rendering data from table
  71.         function loadTodoItems(tx, rs) {
  72.           console.log('data select returned okay');
  73.           var rowOutput = "";
  74.           var todoItems = document.getElementById("todoItems");
  75.           for (var i=0; i < rs.rows.length; i++) {
  76.             rowOutput += renderTodo(rs.rows.item(i));
  77.           }
  78.  
  79.           todoItems.innerHTML = rowOutput;
  80.         }
  81.        
  82.         function renderTodo(row) {
  83.           return "<li>" + row.todo +
  84.             " [<a href='javascript:void(0);' onclick=\'html5rocks.webdb.deleteTodo(" +
  85.             row.ID +");\'>Delete</a>]</li>";
  86.         }
  87.        
  88.         //deleting data from a table
  89.         html5rocks.webdb.deleteTodo = function(id) {
  90.           var db = html5rocks.webdb.db;
  91.           db.transaction(function(tx){
  92.             tx.executeSql("DELETE FROM todo WHERE ID=?", [id],
  93.                     function(tx, r){
  94.                       console.log('delete table success');
  95.                     },
  96.                     function(tx, e){
  97.                       console.log('delete table error '+ e.message);
  98.                     });
  99.             });
  100.         }
  101.        
  102.         //when the page loads open data and do crud
  103.         function init() {
  104.           html5rocks.webdb.open();
  105.           html5rocks.webdb.createTable();
  106.           html5rocks.webdb.getAllTodoItems(loadTodoItems);
  107.         }
  108.        
  109.         function addTodo() {
  110.           var todo = document.getElementById("todo");
  111.           html5rocks.webdb.addTodo(todo.value);
  112.           todo.value = "";
  113.         }
  114.        
  115.         </script>
  116.     </head>
  117.     <body onload="init();">
  118.     <ul id="todoItems">
  119.     </ul>
  120.         <div>TODO write content</div>
  121.        
  122.         <form type="post" onsubmit="addTodo(); return false;">
  123.           <input type="text" id="todo" name="todo" placeholder="What do you need to do?" style="width: 200px;" />
  124.           <input type="submit" value="Add Todo Item"/>
  125.         </form>
  126.     </body>
  127. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement