Skip to content

Instantly share code, notes, and snippets.

@Witiko
Created September 6, 2013 11:05
Show Gist options
  • Save Witiko/6462414 to your computer and use it in GitHub Desktop.
Save Witiko/6462414 to your computer and use it in GitHub Desktop.
User data (IE-only data storage technique) interfacing library
/*
EasyUserData library
A lightweight solution for basic userData handing
Copyright (C) 2010 Vít Novotný
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Requires:
Witiko's Compatible Now! Library
Usage:
Global Object / Boolean false userData
userData.save(name, data);
userData.load[name];
userData.erase(name);
userData.length
*/
var userData = ((document.documentElement || document.getElementsByTagName("html")[0]).addBehavior)?(function() {
var userDataElement = document.documentElement || document.getElementsByTagName("html")[0],
loaded = {}, prefix = "data-", list, length = 0;
userDataElement.addBehavior("#default#userData");
userDataElement.load("userData");
return {
save : function(name, data) {
if(!userData.load[name]) {
userData.length++;
list.push(name);
}
userData.load[name] = data;
userDataElement.load(prefix + name);
userDataElement.setAttribute("userData", data);
userDataElement.save(prefix + name);
userDataElement.load("userData");
userDataElement.setAttribute(prefix + "list", list.join(";"));
userDataElement.save("userData");
return true;
},
erase : function(name) {
if(!userData.load[name]) return false;
list.removeByValue(name);
userData.length--;
delete userData.load[name];
userDataElement.load(prefix + name);
userDataElement.removeAttribute("userData");
userDataElement.save(prefix + name);
userDataElement.load("userData");
userDataElement.setAttribute(prefix + "list", list.join(";"));
userDataElement.save("userData");
return true;
},
load : (function() {
list = userDataElement.getAttribute(prefix + "list");
if(!(String.isString(list))) {
list = [];
} else {
list = list.split(";");
for(var i = 0, l = list.length; i < l; i++) {
userDataElement.load(prefix + list[i]);
loaded[list[i]] = userDataElement.getAttribute("userData");
length++;
userDataElement.load("userData");
}
}
return loaded;
})(),
length : length
}
})():false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment