Post by Peter on Dec 10, 2006 8:41:37 GMT -8
Hopefully this will be of some use to coders.
Ok, basically it stores data which is persistent. By persistent, I mean that the value will stay forever, so if you were to restart your browser, then the data can be recalled.
This will work in Firefox 2 and IE 6 +.
This is the code, it should be placed into the global header so you have access to it across the whole forum.
<script type="text/javascript">
<!--
function Storage(db){
this.db = db;
this.ff = (document.attachEvent)? false : true;
this.ff2 = (typeof globalStorage != "undefined")? true : false;
this.host = location.host;
this.gid = function(id){
return document.getElementById(id);
}
if(!this.ff){
this.storage_objs();
}
}
Storage.prototype.storage_objs = function(){
var sobj = document.createElement("input");
sobj.type = "hidden";
sobj.id = this.db;
sobj.className = "userData";
document.body.appendChild(sobj);
var sheet = document.styleSheets.item(0);
if(sheet){
var ud = false;
for(r = 0; r < sheet.rules.length; r ++){
if(sheet.rules.item(r).selectorText == ".userData"){
ud = true;
break;
}
}
if(!ud){
sheet.addRule(".userData", "behavior: url(#default#userdata)");
}
}
}
Storage.prototype.save = function(value){
if(value){
if(this.ff && this.ff2){
var pb_storage = globalStorage[this.host];
pb_storage[this.db] = value;
} else if(!this.ff){
this.storage_objs(this.db);
this.gid(this.db).setAttribute("pb_pst", value);
this.gid(this.db).save(this.db);
}
return true;
}
return false;
}
Storage.prototype.load = function(){
var value;
if(this.db){
if(this.ff && this.ff2){
var pb_storage = globalStorage[this.host];
value = pb_storage[this.db];
} else if(!this.ff){
this.storage_objs(this.db);
this.gid(this.db).load(this.db);
value = this.gid(this.db).getAttribute("pb_pst");
}
}
return value;
}
//-->
</script>
To use it we create a new instance of the Storage object, at the same time we pass a unique name for the storage database.
Example:
var posts = new Storage("posts_db");
We have now created a database where we can store and retrieve values.
To save a value to the database we use the save method like so....
posts.save("5,563");
When run, the string "5,563" will be saved.
At anytime on the forum we can call up this value by calling the load method like so...
posts.load();
Remember, if you haven't created an instance of the object and you wish to load a value, then you will need too do this...
var posts = new Storage("posts_db");
alert(posts.load());
That will alert the posts.
Reason why you might do that is you may be wanting to save a value that is on the main page only (main page header or footer), and then wish to call that value inside a board.
Another good thing is you can create multiple instances of the Storage object so you can store more than 1 item.
Suggestions? Problems? Not understanding?
Post below
Ok, basically it stores data which is persistent. By persistent, I mean that the value will stay forever, so if you were to restart your browser, then the data can be recalled.
This will work in Firefox 2 and IE 6 +.
This is the code, it should be placed into the global header so you have access to it across the whole forum.
<script type="text/javascript">
<!--
function Storage(db){
this.db = db;
this.ff = (document.attachEvent)? false : true;
this.ff2 = (typeof globalStorage != "undefined")? true : false;
this.host = location.host;
this.gid = function(id){
return document.getElementById(id);
}
if(!this.ff){
this.storage_objs();
}
}
Storage.prototype.storage_objs = function(){
var sobj = document.createElement("input");
sobj.type = "hidden";
sobj.id = this.db;
sobj.className = "userData";
document.body.appendChild(sobj);
var sheet = document.styleSheets.item(0);
if(sheet){
var ud = false;
for(r = 0; r < sheet.rules.length; r ++){
if(sheet.rules.item(r).selectorText == ".userData"){
ud = true;
break;
}
}
if(!ud){
sheet.addRule(".userData", "behavior: url(#default#userdata)");
}
}
}
Storage.prototype.save = function(value){
if(value){
if(this.ff && this.ff2){
var pb_storage = globalStorage[this.host];
pb_storage[this.db] = value;
} else if(!this.ff){
this.storage_objs(this.db);
this.gid(this.db).setAttribute("pb_pst", value);
this.gid(this.db).save(this.db);
}
return true;
}
return false;
}
Storage.prototype.load = function(){
var value;
if(this.db){
if(this.ff && this.ff2){
var pb_storage = globalStorage[this.host];
value = pb_storage[this.db];
} else if(!this.ff){
this.storage_objs(this.db);
this.gid(this.db).load(this.db);
value = this.gid(this.db).getAttribute("pb_pst");
}
}
return value;
}
//-->
</script>
To use it we create a new instance of the Storage object, at the same time we pass a unique name for the storage database.
Example:
var posts = new Storage("posts_db");
We have now created a database where we can store and retrieve values.
To save a value to the database we use the save method like so....
posts.save("5,563");
When run, the string "5,563" will be saved.
At anytime on the forum we can call up this value by calling the load method like so...
posts.load();
Remember, if you haven't created an instance of the object and you wish to load a value, then you will need too do this...
var posts = new Storage("posts_db");
alert(posts.load());
That will alert the posts.
Reason why you might do that is you may be wanting to save a value that is on the main page only (main page header or footer), and then wish to call that value inside a board.
Another good thing is you can create multiple instances of the Storage object so you can store more than 1 item.
Suggestions? Problems? Not understanding?
Post below