Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookies question

Status
Not open for further replies.

Gmon

IS-IT--Management
Mar 18, 2001
14
0
0
US
Can't a cookie be set by just placing document.cookie in the header or body? Or, does it need to be in a function? If it needs to be in a function, does the cookie function have to called based on an Event handler?

Thanks

 
Don't know about cookies, but I don't know why it would HAVE to be in a function to work -- should just execute just like it would if you were declaring a global array or anything else.

as far as functions go, yes, all javascript functions have to be fired by some event -- hence an event-driven language

good luck! :)
Paul Prewett
 
Cookies are set server side by writing them into the HTTP header in a requested document, though that is obviously outside the realm of client-side Javascript, which it appears you're talking about.

No, functions don't have to be called from event handlers, although this is often how they are called. Functions can also be called from other functions and can be invoked from some inline source:

<script language=&quot;javascript&quot;>
<!--
function foo() { alert(&quot;hello&quot;); }

foo();
//-->
</script>

To write a cookie client-side using Javascript, you just have set document.cookie appropriately, it's unimportant if you do this in a function or not.

Russ
bobbitts@hotmail.com
 
So, if I want to set a cookie outside a function, would this work. If not, what would?

<head>

<script language=&quot;javascript&quot;>
document.cookie=&quot;test&quot;
</script>

</head>

Thanks
 
Personally, I'd do it at the top of the body, not the header.

If you want to make it a function, it can be called from the onLoad event.

Rose/Miros
 
Why doesn't this routine work? I can get the cookie to set using other more elborate ways, but I would like to just use something simple. What am I missing here with this routine?

Thanks for your help.

<head>

<script language=&quot;javascript&quot;>
function setcookie() {
var name
name=&quot;Test&quot;
document.cookie=&quot;testname=&quot;+name
document.write(&quot;Cookie Set&quot;)
}
setcookie();

</script>
</head>
 
I think the problem is that since you're not setting an expiration date, the cookie is not being saved.

Change your code to the following and it should work:
Code:
<head>

<script language=&quot;javascript&quot;>
function setcookie() {
    var name
    name=&quot;Test&quot;
    document.cookie=&quot;testname=&quot;+name + &quot;; expires=Monday, 01-Apr-2002 00:00&quot;
    document.write(&quot;Cookie Set&quot;)
        }
setcookie();

</script>
</head>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top