javascript - Check a Boolean value from database at the start of an app then load terms and condition page if false, load menu page if true -
is possible?
before page loads, run javascript code check value in database is, open relevant page?
here code far:
function ondeviceready() //phone gap ready { try { db = opendatabase('cuopioid', '1.0', 'cupioids application database', 2 * 1024 * 1024); } catch (e) { // error handling code goes here. if (e == invalid_state_err) { // version number mismatch. alert("invalid database version."); } else { alert("unknown error " + e + "."); } } console.log("database created!"); db.transaction(createtableterms, errcb, console.log("medicines table created!")); $('#btnaccept').on('click', acceptterms); }
the code below creates database (if isn't there already)
function createtableterms(tx) { tx.executesql('create table if not exists terms (id unique, status boolean)'); tx.executesql('insert terms (id, status) values (0, 0)'); console.log("terms table created!"); }
i have created function selects rows form database if set true, if results has 1 record want open menu page, if has no results means terms , conditions haven't been accepted previously:
function checkterms(tx) { db.transaction(function(tx) { tx.executesql('select * terms status=1', [],console.log("check terms"), errcb); }); var len = results.rows.length; if (len = 1) { console.log("accepted - menu page"); } else { console.log("termsandconditions page"); } }
i not sure put checkterms(tx) function
i solved problem creating new javascript file runs when html page lodes (start of <head>
tag
- which opens database
- runs sql query find value
- opens relevant page depending on result
it still displays page (half second @ most) when load page, want
here code:
try { db = opendatabase('cuopioid', '1.0', 'cupioids application database', 2 * 1024 * 1024); } catch (e) { // error handling code goes here. if (e == invalid_state_err) { // version number mismatch. alert("invalid database version."); } else { alert("unknown error " + e + "."); } } checkterms(); function checkterms(tx) { db.transaction(function(tx) { tx.executesql('select * terms status=1', [], openpage, errcb); }); } function openpage(tx, results) { var len = results.rows.length; if (len = 1) { getdrugs(); window.location.href = "#page1" console.log("accepted - menu page"); } else { window.location.href = "#page0" console.log("termsandconditions page"); } }
Comments
Post a Comment