Question

I am trying to implement html5 local storage in a standalone xul application, I would like to use local-storage because I think the Xpcom way looks to complicated.

I have read that for a Firefox extension you can do:

document.content.localStorage.setItem("myvariable", "myvalue");
var item = document.content.localStorage.getItem("myvariable");
document.getElementById("result").innerHTML=item;

But this does not work for my standalone application, so how do I do it?

Was it helpful?

Solution

I have found a workaround here.

var url = "http://example.com";
var ios = Components.classes["@mozilla.org/network/io-service;1"]
          .getService(Components.interfaces.nsIIOService);
var ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
          .getService(Components.interfaces.nsIScriptSecurityManager);
var dsm = Components.classes["@mozilla.org/dom/storagemanager;1"]
          .getService(Components.interfaces.nsIDOMStorageManager);

var uri = ios.newURI(url, "", null);
var principal = ssm.getCodebasePrincipal(uri);
var storage = dsm.getLocalStorageForPrincipal(principal, "");

storage.setItem("chromekey", "chromevalue");

What it does is that it pretends that it is example.com so Mozilla lets it assess loaclstorage.

OTHER TIPS

Try this:

var win = document.content.defaultView;
win.localStorage.setItem("myvariable", "myvalue");
var item = win.localStorage.getItem("myvariable"); // why not win.localStorage["myvariable"]
document.getElementById("result").innerHTML=item;

localStorage is property of window object while you are trying to access it with document object

Note that this data would only be accessible from the documents of same domain where it was saved. So if win.document is from www.yahoo.com when you put a value in localStorage, win.localStorage.getItem("myvariable") will only return value for documents from www.yahoo.com/*

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top