/    Sign up×
Community /Pin to ProfileBookmark

JavaScript ingenue – cookies

Hi, whilst I have been developing web sites for a while, I have managed to avoid getting my hands dirty with JS, other than calling other folk’s scripts, for now. However, I need to do so now.

I use overLib to produce tooltips on my pages but some users have asked to turn them off. So I added a button to the page which turns them off. This works by resetting a variable to False (the variable test has been added to my overLib script – I managed *that* much on my own). Of course, as soon as the page is refreshed, the variable is back to the original value (since it’s normally ‘True’ for most users).

So, I thought, simply use a cookie so that, when the page loads, it reads the cookie and acts accordingly. This is where I get lost. I have a set of functions for setting and reading the cookie (below) so that’s not the problem. The nub is, I want to use that button like a toggle, but at the same time, read/set the cookie each time it’s clicked OR the page is loaded:

page loads
set tooltip variable as True (as the default)
read cookie (cookie function tests if cookie is present)
if ‘on’, do nothing
if ‘off’, set tooltip variable for overLib to ‘False’

button clicked
read cookie
toggle cookie (i.e. if ‘on’, set to ‘off’ and vice-versa)

Here’s what I have so far.

  • they’re separate script sections because my brain hurts following too much code


  • ShowToolTips is what overLib tests for so the line ‘ShowToolTips=!ShowToolTips’ is where the action should be, I guess.

    <SCRIPT LANGUAGE=’JAVASCRIPT’>
    // delay in msecs
    var ol_delay = ‘500’

    var ShowToolTips

    tooltip_cookie_name = “AppDB_TooltipToggle”
    TooltipToggle

  • </SCRIPT>

    <SCRIPT LANGUAGE=’JAVASCRIPT’>
    function TooltipToggle()
    {

    var tipToggle = readCookie(tooltip_cookie_name)
    if (tipToggle)
    {
    ShowToolTips=!ShowToolTips
    }
    else
    createCookie(tooltip_cookie_name,”on”,365)

    }

    function createCookie(name,value,days)
    {
    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+”=”+value+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
    }

    function eraseCookie(name)
    {
    createCookie(name,””,-1)
    }

    </SCRIPT>

    to post a comment
    JavaScript

    5 Comments(s)

    Copy linkTweet thisAlerts:
    @7studJan 24.2005 — 1) tooltip_cookie_name = "AppDB_TooltipToggle"
    [color="red"]TooltipToggle [/color]

    That's not a javascript statement.

    2) js lines end in semicolons. Although not strictly required in every situation, it's good practice to use them.

    3)var ShowToolTips
    ....
    if (tipToggle)
    {
    ShowToolTips=!ShowToolTips
    }

    After the first line, ShowToolTips is undefined. Then, when the other line is encountered, you set ShowToolTips to !undefined, which works but it's not exactly clear. Why not do this instead:
    var ShowToolTips = true;
    ....
    if (tipToggle)
    {
    ShowToolTips=!ShowToolTips
    }

    Then, the only problem is: when the page reloads, ShowToolTips will be set to true even when there is a cookie. So, check for the cookie before declaring ShowToolTips--something like this:
    var tipToggle = readCookie(tooltip_cookie_name);
    if(!tipToggle) var ShowToolTips = true;
    else ShowToolTips = tipToggle;

    After those lines, ShowToolTips should indicate whether the user wants tooltips or not.
    Copy linkTweet thisAlerts:
    @IANNORTHWOODauthorJan 24.2005 — Sorry to be a pain, but can you put the new code in context? Do I still need the function TooltipToggle? If so, how do I call it (you said that the simple 'TooltipToggle' isn't a vlid statement).

    Also, where does 'createCookie' fit in? Presumably, I need to switch its contents each time the button gets clicked, right?
    Copy linkTweet thisAlerts:
    @7studJan 24.2005 — Huh? Of course TooltipToggle() is still needed. How else are your users going to be able to change their preference for tooltips? The default value for tooltips is true, and the users need to be able to change that to false, or even back to true at some later date. That requires a button whose onclick event calls TooltipToggle().

    Also, these two lines:

    var ShowToolTips = true;

    ShowToolTips=!ShowToolTips;

    result in ShowToolTips being false, so they are equivalent to:

    var ShowToolTips = false;

    Is that what you want?
    Copy linkTweet thisAlerts:
    @7studJan 24.2005 — where does 'createCookie' fit in?[/quote]
    When the user clicks on the button. Check to see if a cookie exists, if it does, toggle it's value, if it doesn't create it and assign it a value.

    Also, I think you have to consider: what if your user first decides they don't want tooltips, and then later they want to turn them back on? Do you want one 'toggle' button which might be confusing to your users, or do you want a 'yes' button and a 'no' button. If it's the latter, you wouldn't need a toggle function. You would use the onclick event of the 'yes' button to set ShowToolTips to true, and you would use the onclick event of the 'no' button to set ShowToolTips to false.
    Copy linkTweet thisAlerts:
    @Warren86Jan 24.2005 — Ian:

    The following code uses a "session only" cookie, meaning it isn't written to the user's disk, and is good only for the current browsing session.

    <HTML>

    <Head>

    <Script Language=JavaScript>

    var showToolTips = true;

    function toggleTip(){

    if (getCookie() == 'on'){setCookie('off');showToolTips = false}
    else if (getCookie() == 'off'){setCookie('on');showToolTips = true}
    }

    function getCookie(){

    isStr = document.cookie;
    startSlice = isStr.indexOf("=");
    endSlice = isStr.length;
    isValue = isStr.slice(startSlice+1,endSlice);
    return isValue;
    }

    function setCookie(value){

    curCookie = "toolTip"+ "=" +value;
    document.cookie = curCookie;
    }

    function initTip(){

    if (!getCookie()){setCookie('on')}
    else {if (getCookie() == 'off'){showToolTips = false}}
    }

    window.onload=initTip;


    </Script>

    </Head>

    <Body>

    <input type=button value="Toggle Tooltip" onclick="toggleTip()"><br><br>

    <input type=button value="Read Variable" onclick="alert(showToolTips)"><br><br>

    <a href='1.html'> Some Link </a>

    </Body>

    </HTML>
    ×

    Success!

    Help @IANNORTHWOOD spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...