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
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>