Beginner questions about making Java script cookie
kenneth_ltk
Status: New User - Welcome
Joined: 30 Jan 2007
Posts: 1
Reply Quote
Hello,

I'm completely new in web programming and I just made a Google search and find this tutorial on how to make a javascript cookie (http://techpatterns.com/downloads/javascript_cookies.php).

I have problems on how to make the script really work for my website and please help me if you have related experience, thank you.

1) There are several variables need to be replaced like name, value, expires, path, domain, secure, I don't understand the meaning of "value", what should I put in the variable "value"?

2) Do I need to create a file something like "cookie.js" and put it in my Public_html folder in order to make the cookie work properly?

3) I have tried to put the following script into my head tag, please tell me if something is wrong. I only made changes in the five variables. I tried to make the cookie expires after 24 hrs.

function Get_Cookie( name ) {

var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

function Set_Cookie( test, testing, 1, '/', '', '' )
{
var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60 * 60 * 60;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


If u have experience on writing a cookie script, please help me, thank you very much.

Regards,
Kenneth
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
Don't change the functions in any way, do not insert values into their fields or you destroy them, like you did.

Please take some time to learn before you try something complicated like setting cookies, calling functions etc.

Read, study, practice.

A function has to be called on the page, don't touch the function code itself, notice that what you posted is different than what you downloaded from our site: function Set_Cookie( test, testing, 1, '/', '', '' )
You broke the code since you randomly inserted new variable names without having any idea of what you are doing. Don't mess with the code you download unless you have some idea of what you are doing, you'll just break it.

The cookie code does not require any modification at all, and should not be touched.

What you did there destroys the function, since you have changed the name of the variables which will be used in that function.

Don't ever change programming in any way until you understand it.

Read up on functions and how to call them, then once you have some grasp of the material feel free to come back and ask questions, but we don't really do newbie tutorials here, it's not a good way for us to spend our time. Other places do.

But read, practice study. If you can't do something simple like:

function something()
{
document.write('hello world');
}
something;

you can't do anything more complex, so don't try until you spend some days / weeks reading up on basic beginner javascript programming concepts.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours