View Single Post
Old 01-23-2013, 08:19 AM   PM User | #1
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 780
Thanks: 41
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
global variables using jstorage

I am trying out a javascript storage system called jstorage. It is like cookies but without the restrictions. I can insert a value and resurrect it later with:

Code:
insert_value();
get_value();
I am now using it in a script to store the fields of inputs and instantly update them in the page. It works!

Code:
window.onload = function()
{
	var theInputs = document.getElementsByTagName('input');
	for(var i = 0; i < theInputs.length; i++)
	{
		if(theInputs[i].type == 'text')
		{
			theInputs[i].onkeyup = function()
			{
				validate();insert_value(this.name,this.value);
			}
		}
		get_value('done');
		if(value != null)
		{
			theInputs[i].value = value;
		}
	}
}

function insert_value(key, val){
$.jStorage.set(key, val);
var key = "",val = "";
}

function get_value(key){
var value = $.jStorage.get(key);
var key = "";
}
But what I want to do is a bit more complicated. It has to do with global variables, I think.

I want to get the value of a previously stored item and use it with the value of the item displayed in the code above e.g:

Code:
get_value('something') + get_value('done')...

i.e. fred + y
...and then save this combined value using the jstorage insert_value() function to be retrieved later.

The question is not a jstorage issue (as far as I can see), but a way to use the retrieved get_values in the windows onload function.

Everything I try doesn't let me use the values retrieved which become undefined or null when I try to combine them.

Can anyone see what I am doing wrong? Here is the entire page:

Code:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Test</title>
<script src="js/json2.js"></script>
<script src="js/jstorage.min.js"></script>
<script>
window.onload = function()
{
	var theInputs = document.getElementsByTagName('input');
	for(var i = 0; i < theInputs.length; i++)
	{
		if(theInputs[i].type == 'text')
		{
			theInputs[i].onkeyup = function()
			{
				validate();insert_value(this.name,this.value);
			}
		}
		get_value('done');
		if(value != null)
		{
			theInputs[i].value = value;
		}
	}
}

function insert_value(key, val){
$.jStorage.set(key, val);
//alert(key);alert(val);
var key = "",val = "";
}

function get_value(key){
var value = $.jStorage.get(key);
alert(value);
var key = "";
}

get_value('lesson');
get_value('done');

</script>
</head>

<body>

<table border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td>
		<table class="write" border="0" style="border-collapse: collapse" cellpadding="3">
			<tr>
				<td>
				<input type="text" id="done" name="done" size="2" maxlength="1" value="N"></td>
			</tr>
		</table>
		</td>
	</tr>
</table>

</body>

</html>
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
tpeck is offline   Reply With Quote