开发者

WebSQL and localStorage Scope in Chrome

开发者 https://www.devze.com 2023-03-16 02:22 出处:网络
EDIT: The code below works correctly actually. Chrome developer tools UI simply does not show the values in local & webSQL storage after a page load, though they are available to my scripts.

EDIT: The code below works correctly actually. Chrome developer tools UI simply does not show the values in local & webSQL storage after a page load, though they are available to my scripts.

My login page saves some authentication data locally with WebSQL / localStorage, then I redirect to a landing page where that local data promptly disappears. I'm not sure why the data does not persist across page loads (all pages are on the same domain/protocol/port). I'm testing in Chrome 12.0.742.100 on Ubuntu. Here's my code, any ideas?

var db = window.openDatabase("session", "1.0", "session", 1000000);
db.transaction(function(tx){
   tx.executeSql('CREATE TABLE IF NOT EXISTS auth (uuid UNIQUE TEXT, token TEXT)');
});
db.transaction(function(tx){
   tx.executeSql('INSERT INTO auth (uuid, token) values (?, ?)',[json.uuid,json.token]);
});
localStorage.setItem("uuid",json.uuid);
localStorage.setItem("token",json.token);

// REDIRECT TO HOMEPAGE
window.location = 'droid.html';

//... the next page has an alert() that tries to access the lo开发者_如何转开发cal values.


It could be that you have a scoping problem, or that the values aren't actually being set.

Here are a few things I'd recommend trying (in this order):

  1. prove that the data is actually being set in localStorage by retrieving it immediately, on the same page;
  2. prove that the data is persisting (at least for a little while) by retrieving it, on the same page, after a timeout of, say, 2 seconds;
  3. instead of setting window.location, try using window.location.assign()
  4. instead of changing the window location immediately, do it after a short timeout (say 100ms), or after the page loads (if the page hasn't finished loading already).

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消