Forum Moderators: open
could someone perhaps provide me a sample code that will take the name=value pairs from the cookie and add them to a hidden field?
I'm new to javascript so I have no idea. (I'm learning perl already so javascript will have to wait). I've tried searching for it but no luck so far. I did stumble upon quite a few tutorials and I think that you use windows.location.search to get the name=value pairs and innerhtml to set the value of the hidden field. Am I close? Anyways, a working example is the way I learn best so if someone could provide me with one that would be greatly appreciated.
document.myform.myfield.value = variable;
Make sure your form and fields are named, so Javascript can fiend them, i.e.
<form name=myform>
<input type=hidden name=myfield>
What am I doing wrong?
<HTML>
<HEAD>
<script type="text/javascript">
<!--
function querySt() {
hu = window.location.search.substring(1);
}
document.myform.myfield.value = hu;
}
-->
</script>
</HEAD>
<BODY onload="querySt">
<form name="myform" method="post" action="">
<input type="hidden" name="myfield">
</form>
</BODY>
</HTML>
function DoTheCookieStuff()
{
username=getCookie('username');
if (username!=null) {alert('Hi there '+username+' - Good to see you again!')}
else {username=window.location.search.substring(1);setCookie('username',username,365)}
}
As you can see there's a if/else in there. But I don't need an if/else. I just need the if in there and get rid of the else. So I changed it to the following:
function DoTheCookieStuff()
{
username=getCookie('username');
if (username=null) {username=window.location.search.substring(1);setCookie('username',username,365)}
}
After doing that the script doesn't work anymore. Can anyone help me figure out why?