All is working good inside the webworker.
I can create the db, create tables, insert data then show the data
using select then posting the looped results back to the html page
Is it only possible to access the database created in the worker from
that worker?
I have used the same code out of the web worker in a js function on
the html page to select and show the information.
Using this code to access db (the database created in web worker) it
comes up as 'db is not defined'
Is there a way to access the database created in the web worker from a
js function in the html page, or must every request to the database be
made from the html page via postMessage(), then web worker accesses
database, gets results and postMessage()s the results back to be html
page??
Here is code for example I am using:
HTML:
<html>
<head>
<script>
dbconf=new Worker('wwdbconftest.js');
dbconf.onmessage=function(event){
//alert(event.data);
document.getElementById('display').innerHTML+=event.data+'<br>';
}
function showDBContent(){
var query='select * from locations';
db.transaction(function(tx){
result=tx.executeSql(query);
if(result.rows.length>0){
for(var l=0; l<result.rows.length; l++){
document.getElementById('display').innerHTML+='Result:
'+result.rows.item(l)['id']+'<br>';
}}});
}
</script>
</head>
<body>
Web Worker DB Conf Test Page<br>
<button onclick="dbconf.postMessage('cbos_npg');">Run dbconf Worker</
button><br><br>
<div id="display">Show Message Responses Here<br></div>
<button onclick="showDBContent();">Show DB Content</button><br>
</body>
</html>
wwwdbconftest.js CODE:
onmessage = function(e) {
var data = e.data;
db = openDatabaseSync("WorkerFoo", "", "", 1);
if(db) postMessage('Web Worker DB Built');
//var query='drop table if exists test';
//db.transaction( function(tx){ tx.executeSql(query, [],
null,null); });
var query='create table if not exists test (id int(10))';
db.transaction( function(tx){ tx.executeSql(query, [], null,null); });
postMessage('Table Created');
var query='insert into test (id) values (1)';
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){
for(var l=0; l<result.rows.length; l++){
postMessage('Result: '+result.rows.item(l)['id']);
}}});
}
Thanks