Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-20-2009, 01:50 PM   PM User | #1
briain
New to the CF scene

 
Join Date: Jun 2009
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
briain is an unknown quantity at this point
Cool Absolute Beginner Cookie help?

Hey Guys

Im wondering if anyone has any good links or something to a JS Cookie 'how to'... with a working example?

I just need the user's text input in a textbox to be auto populated on load on the 2nd time and so on..

any ideas would be really appreciated!!
Briain, Ireland
briain is offline   Reply With Quote
Old 07-20-2009, 01:57 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Try this:-

Code:
<script type = "text/javascript">

var expDate = new Date();
expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year ahead

function setCookie(isName,isValue,dExpires) {
document.cookie = isName + "=" + isValue + ";expires=" + dExpires.toGMTString() + "path=/";
}

function getCookie(isName){
cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1) {return false}
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}

function dispCookie(isName) {
nValue = getCookie(isName);
alert(nValue);
}

function deleteCookie(isName){
if (getCookie(isName)){
document.cookie = isName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}

</script>

</head>

<body>
<input type = "button" value = 'Set Cookie' onclick = "setCookie('anyName','Hello',expDate)">

<br><br>
<input type = "button" value='Delete Cookie' onclick="deleteCookie('anyName')">

<br><br>
<input type = "button" value = 'Read Cookie' onclick = "dispCookie('anyName')">

</body>

"To get back my youth I would do anything in the world, except take exercise, get up early, or be respectable." - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)
Philip M is offline   Reply With Quote
Old 07-20-2009, 03:53 PM   PM User | #3
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Cookie info:

http://www.w3schools.com/JS/js_cookies.asp

These are the functions I use:

Code:
/***********************************************************************/

function get_cookie(cookie_name)
{
        var count = 0;
        var cname = cookie_name+'=';
        var cpos = document.cookie.indexOf(cname);

        if (cpos == -1)
        {
                return '';
        }
        else
        {
                var cookie_vars = document.cookie.split(';');

                while (count < cookie_vars.length)
                {
                        var current = cookie_vars[count];
                        var cpos = current.indexOf(cname);
                        var cstart = cpos+cname.length;

                        if (cpos != -1)
                        {
                                var cookie_info = current.substring(cstart);
                                return unescape(cookie_info);
                        }
                        count++;
                }
                return '';
        }
}

/***********************************************************************/

function set_cookie(cookie_name, values, path, expire)
{
        var domain = '';

        if (expire && expire != '')
        {
                expire = '; expires='+expire;
        }
        else
        {
                expire = '';
        }

        if (cookie_path && cookie_path != '')
        {
                path = '; path='+cookie_path;
        }
        else if (path && path != '')
        {
                path = '; path='+path;
        }
        else
        {
                path = '; path=/';
        }
        document.cookie = cookie_name+'='+values+path+expire+domain+';';
}

/***********************************************************************/

function delete_cookie(cookie_name)
{
        var date = new Date();
        var expire = date.getTime()-900;

        set_cookie(cookie_name, '', '', expire);
}

/***********************************************************************/

p.s: Yes, I do know I have a quirky coding style.
MattF is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:09 AM.


Advertisement
Log in to turn off these ads.