...

View Full Version : Cookies help... If I can't do this I'm doomed! This is for my site...



Striker14537
10-20-2006, 11:08 PM
Hi,
I write this script:

<FORM name="frm1">
<input id="Text1" name="txt1" style="width: 65px" type="text" onchange='document.sector1="Your folder name is: "+document.frm1.txt1.value+". Personalize this webpage for your needs! Cookies must be enabled."' onBlur='alert(document.sector1)' maxlength="10"/>
</FORM>

I want to make it so the cookie gets saved to the user's computer, and even if the user goes to a new page and returns, the text box value is the same as the user entered. Is this possible?

Thanks a million!

Striker

_Aerospace_Eng_
10-20-2006, 11:21 PM
Its setup to work with any number of text inputs.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<script type="text/javascript">
function createCookie(name,whatval,days)
{
if(value != '')
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"val="+whatvalue+expires+"; path=/";
}
}

function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
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()
{
createCookie(this.name,this.value,9999);
}
}
if(readCookie(theInputs[i].name+'val') != null)
{
theInputs[i].value = readCookie(theInputs[i].name+'val');
}
}
}
</script>
</head>
<body>
<form action="#">
<input type="text" name="txt1">
<input type="text" name="txt2">
<input type="text" name="txt3">
</form>
</body>
</html>

Striker14537
10-21-2006, 12:41 AM
It doesn't work for me :( Help?

jmrker
10-21-2006, 02:55 AM
You have 3 different names for 1 variable
Change:


function createCookie(name,whatval,days)
{
if(value != '')
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"val="+whatvalue+expires+"; path=/";
}
}
Worked for me when I used 'whatvalue' instead of 'whatval' and 'value' for consistancy

_Aerospace_Eng_
10-21-2006, 03:00 AM
My fault. As stated I mixed up variable names. This should work

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<script type="text/javascript">
function createCookie(name,whatval,days)
{
if(whatval != '')
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"val="+whatval+expires+"; path=/";
}
}

function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
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()
{
createCookie(this.name,this.value,9999);
}
}
if(readCookie(theInputs[i].name+'val') != null)
{
theInputs[i].value = readCookie(theInputs[i].name+'val');
}
}
}
</script>
</head>
<body>
<form action="#">
<input type="text" name="txt1">
<input type="text" name="txt2">
<input type="text" name="txt3">
</form>
</body>
</html>

Striker14537
10-21-2006, 03:26 AM
It works! You guys are the best! Thank you!

tpeck
10-21-2006, 04:22 AM
Aerospace, this seems to work for my cookie requirement too (interesting how it doesn't require a submit!)

But there seem to be two limitations:

1) it works in FF but sets up a completely separate cookie for this browser - is this normal? Does FF read different cookies?
2) any <body onload="whatever()"> don't work any more.

If these two problems could be overcome, I could dispense with masses amounts of coding on my 1000 pages!

_Aerospace_Eng_
10-21-2006, 07:19 AM
1) The cookie will be set according to how the browser works, don't worry about this part.
2) You can only have one onload on a page. So if you have one in your body get rid of it and move the function call in with the previous window.onload after the for loop.

tpeck
10-21-2006, 03:16 PM
OK - I guess I will have to live with the browser differences with regard to cookies, but I have learnt something: you can set up a site to work a particular way saving any number of cookies, then switch browsers and it's as if your cookies never existed! That seems a fairly daunting prospect if, as I am doing, one is setting up a very large rating system. Perhaps I will have to warn the user not to switch browsers half way through working with the site.

The onload advice worked perfectly. Thanks. In fact, the whole thing works beautifully - I wish I could show you. The ratings I am gathering display like magic without even pressing enter, and even without a form on other pages that tabulate and display the entire matrix. It's like a magic trick!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum