Trying to open a database synchronously using JavaScript. My code looks something like this. Whenever I run it on Chrome, I get the following 2 errors
Uncaught SecurityError: Failed to construct 'Worker': Script at 'file:///C:/Users/KH1483/Desktop/web%20sql/worker.js' cannot be accessed from origin 'null'. dbSync.html:23
Uncaught TypeError: Cannot read property 'rows' of undefined dbSync.html:16
<html>
<head>
<script>
function test() {
db = openDatabase("Foo", "", "", 1);
if (db)
alert("Opened Foo");
else
alert(2);
db = openDatabase("WorkerFoo", "", "", 1);
var query='select * from test';
db.transaction(function(tx){
result=tx.executeSql(query);
if(result.rows.length>0){
for(var l=0; l<result.rows.length; l++){
alert('Result: '+result.rows.item(l)['id']);
}
}
});
worker = new Worker('worker.js');
worker.onmessage = function(event) {
alert(event.data);
}
}
</script>
</head>
<body onload="test()">
</body>
</html>
/* Worker.js starts here */
db = openDatabaseSync("WorkerFoo", "", "", 1);
if(db) postMessage('Web Worker DB Synchronous Built');
try {
var query='create table if not exists test (id int(10))';
db.transaction( function(tx){ tx.executeSql(query, [], null,null); });
postMessage('Table Created');
}catch(err){
postMessage("Exception: " + err + "; code = " + err.code);
}
var query='insert into test (id) values (100)';
db.transaction( function(tx){ tx.executeSql(query, [], null,null); });
postMessage('Data Inserted');
var query='select * from test';
db.transaction(function(tx){
result=tx.executeSql(query);
if(result.rows.length>0){
postMessage("Data Length="+result.rows.length);
for(var l=0; l<result.rows.length; l++){
postMessage('Result: '+result.rows.item(l)['id']);
}
}
});