PDA

View Full Version : Sticky Note: With Cookie Write/Read Addition



w0rdie
02-28-2008, 08:19 PM
1) Script Title: Sticky note

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/stickynote.htm

3) Describe problem:

The script is great but I need it to write a cookie that lasts for 24 hours rather than per session-- I've tried to add this functionality but I have only succecced in breaking the script entirely. Help!

ddadmin
02-29-2008, 07:42 AM
Sure, try the below modified script in place of the original (script in the HEAD of your page):


<script type="text/javascript">

/***********************************************
* Sticky Note script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of DHTML scripts
* This notice must stay intact for legal use
* Go to http://www.dynamicdrive.com/ for full source code
***********************************************/

//Specify display mode. 3 possible values are:
//1) "always"- This makes the fade-in box load each time the page is displayed
//2) "onceperday"- This uses cookies to display the fade-in box only once per 24 hr
//3) integer (ie: 5)- Finally, you can specify an integer to display the box randomly via a frequency of 1/integer...
// For example, 2 would display the box about (1/2) 50% of the time the page loads.

var displaymode="onceperday"

var enablefade="yes" //("yes" to enable fade in effect, "no" to disable)
var autohidebox=["yes", 5] //Automatically hide box after x seconds? [yes/no, if_yes_hide_after_seconds]
var showonscroll="yes" //Should box remain visible even when user scrolls page? ("yes"/"no)
var IEfadelength=1 //fade in duration for IE, in seconds
var Mozfadedegree=0.05 //fade in degree for NS6+ (number between 0 and 1. Recommended max: 0.2)

////////No need to edit beyond here///////////

if (parseInt(displaymode)!=NaN)
var random_num=Math.floor(Math.random()*displaymode)

function displayfadeinbox(){
var ie=document.all && !window.opera
var dom=document.getElementById
iebody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
objref=(dom)? document.getElementById("fadeinbox") : document.all.fadeinbox
var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset
var docwidth=(ie)? iebody.clientWidth : window.innerWidth
docheight=(ie)? iebody.clientHeight: window.innerHeight
var objwidth=objref.offsetWidth
objheight=objref.offsetHeight
objref.style.left=docwidth/2-objwidth/2+"px"
objref.style.top=scroll_top+docheight/2-objheight/2+"px"

if (showonscroll=="yes")
showonscrollvar=setInterval("staticfadebox()", 50)

if (enablefade=="yes" && objref.filters){
objref.filters[0].duration=IEfadelength
objref.filters[0].Apply()
objref.filters[0].Play()
}
objref.style.visibility="visible"
if (objref.style.MozOpacity){
if (enablefade=="yes")
mozfadevar=setInterval("mozfadefx()", 90)
else{
objref.style.MozOpacity=1
controlledhidebox()
}
}
else
controlledhidebox()
}

function mozfadefx(){
if (parseFloat(objref.style.MozOpacity)<1)
objref.style.MozOpacity=parseFloat(objref.style.MozOpacity)+Mozfadedegree
else{
clearInterval(mozfadevar)
controlledhidebox()
}
}

function staticfadebox(){
var ie=document.all && !window.opera
var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset
objref.style.top=scroll_top+docheight/2-objheight/2+"px"
}

function hidefadebox(){
objref.style.visibility="hidden"
if (typeof showonscrollvar!="undefined")
clearInterval(showonscrollvar)
}

function controlledhidebox(){
if (autohidebox[0]=="yes"){
var delayvar=(enablefade=="yes" && objref.filters)? (autohidebox[1]+objref.filters[0].duration)*1000 : autohidebox[1]*1000
setTimeout("hidefadebox()", delayvar)
}
}

function initfunction(){
setTimeout("displayfadeinbox()", 100)
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = ""
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset)
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}


if (displaymode=="onceperday" && get_cookie("fadedin")=="" || displaymode=="always" || parseInt(displaymode)!=NaN && random_num==0){
if (window.addEventListener)
window.addEventListener("load", initfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", initfunction)
else if (document.getElementById)
window.onload=initfunction
if (displaymode=="onceperday"){
var expireDate = new Date()
var expstring=expireDate.setDate(expireDate.getDate()+1) //1 day to expire
document.cookie="fadedin=yes; expires="+expireDate.toGMTString()
}
}


</script>

Not extensively tested though, but seems to behave correctly in terms of the 24hr time limit.

w0rdie
02-29-2008, 03:11 PM
Thank you so much!

Everything works except that the cookie seems to still be based on the browser session in IE 6 and 7. :(

ddadmin
02-29-2008, 09:17 PM
Are you sure? The code above was tested in IE7, and the cookie does persist beyond just the browser session for me.

w0rdie
03-04-2008, 02:10 PM
It does infact work! Thank you SO much. :D

Jessic4
09-22-2012, 11:28 AM
This modification looks awesome and handly. I dont know anything about cookies but I'm wondering if it would be possible to add a "Do Not Show This Sticky Note Ever Again" option to not invade the users experiences if they grow irritated of it. Please, could somebody suggest how I would go about this? :)

Beverleyh
09-22-2012, 02:28 PM
That would be a great addition - perfect for one of those 'read our privacy policy and accept cookies' reminders too. Good idea Jessic4 :)

traq
09-22-2012, 02:39 PM
geez... I'm glad I don't live in the UK. I can't imagine how much of a PITA it is to deal with that stupid cookie law

Beverleyh
09-22-2012, 04:43 PM
Yes, it is very tiresome :)

The plus side is that its a convenient reminder to crack open the hob nobs!
(I've learnt my lesson > http://www.mcvities.co.uk/product/hobnobs )

jscheuer1
09-22-2012, 09:35 PM
I've updated the script to do that. See:

http://www.dynamicdrive.com/forums/showthread.php?71077-Sticky-Note-with-24-cookie-and-also-a-quot-never-display-again-quot-option&p=282266#post282266