PDA

View Full Version : Memory Code



Cheeseweasel
12-06-2007, 04:42 PM
I run a forum where I recently used a script to make snow from the sky ('tis the season to be jolly). Unfortunately this script slightly slows down some users' browsers, so I built in a function to stop the snow. The function is as follows:


javascript:snowStorm.stop()

However, this function doesn't work site-wide so the users have to click the button on every page if they don't want to snow. The site doesn't use iFrames (so I can't make the button universal) and I don't intend to make any major adjustments to the site.

Is there any way of making the script remember if the user has clicked the button, even if only for one session? At present I am using an alternate skin which members may choose if they wish, but I'd much rather have a button which they can click and remembers it.

Any ideas?

Thanks
Cheeseweasel

jscheuer1
12-06-2007, 05:23 PM
A javascript cookie would work fairly well for that, they can be hard to use, but:

http://www.quirksmode.org/js/cookies.html

explains it pretty well and provides create, read, and erase cookie functions that do all the heavy lifting and that most beginner to intermediate javascript folks can use. You can set (create) the name, value and days duration of a cookie, read a cookie's value, just see if it exists or not, and erase cookies by name, all with relative ease using these functions.

Basically, the first time someone stops the snow, also create a cookie. Then every time a page with snow on it is loading, check to see if the cookie is there and don't even start the snow if that cookie is present.

boogyman
12-06-2007, 05:25 PM
use a cookie,

you will need to set a cookie for the skin "directory" as well if you want the skin to be remembered when the user comes back at a later time/date

.
oops. looks like john has faster fingers then myself, and alot more descripttive too

Cheeseweasel
12-06-2007, 07:04 PM
Thanks for your help, but I'm not exactly a master coder. Could anybody please give me an example of how to use this code to create a cookie that carries out that function and a script for the webpage to read it?

Master_script_maker
12-06-2007, 10:24 PM
try this:


function snowStorm.stop(set) {
if (set == null) {
var expire=new Date();
expire.setDate(expire.getDate()+5); //expires five days in the future
document.cookie='snowstop=yes; expires=expire.toGMTString; path=/';
}
//the rest of code to stop it
}
function checkcookie() {
var name="snowstop=";
var split=document.cookie.split(";");
for(var i=0;i < split.length; i++) {
var cookievals=split[i];
while (cookievals.charAt(0)==" ") cookievals = cookievals.substring(1,cookievals.length);
if (cookievals.indexOf(name) == 0) return cookievals.substring(name.length,cookievals.length);
}
return null;
}
function bodyload() {
var cookieset = checkcookie();
if (cookieset != null) {
snowStorm.stop(true)
}
and state this in body tag:

<body onload="bodyload()">

Cheeseweasel
12-07-2007, 04:00 PM
Thanks - only problem is that I only want that to happen when a certain button is pressed. How could I do that?

Cheeseweasel
12-07-2007, 05:50 PM
(relocated from thread in the 'Javascript' board)

There is one function on a website I own that makes it "snow". Unfortunately this makes some very old computers work very slowly, so I have added a 'stop snow' button. However, users must press this on every single page and many have raised complaints concerning this.

Here is what I would like:

A JAVASCRIPT code to insert a button which creates a cookie which contains the function "javascript:snowStorm.stop()" and will be used every time the site is visited. It should expire within 2 days.

Please could somebody create a code to create and read the cookie?

Thanks
-Cheeseweasel

Master_script_maker
12-07-2007, 09:01 PM
use this (valid netscape/firefox ver 6.2 and ie ver 4.0):
<button onclick="snowStorm.stop()">Stop Snow</button>
or this (valid netscape/firefox ver 3.0 and ie ver 3.0):

<input type="button" value="Stop Snow" onClick="snowStorm.stop()" />

Master_script_maker
12-07-2007, 11:12 PM
try this:

function snowStorm.stop(set) {
if (set == null) {
var expire=new Date();
expire.setDate(expire.getDate()+2); //expires two days in the future
document.cookie='snowstop=yes; expires=expire.toGMTString; path=/';
}
//the rest of code to stop it
}
function checkcookie() {
var name="snowstop=";
var split=document.cookie.split(";");
for(var i=0;i < split.length; i++) {
var cookievals=split[i];
while (cookievals.charAt(0)==" ") cookievals = cookievals.substring(1,cookievals.length);
if (cookievals.indexOf(name) == 0) return cookievals.substring(name.length,cookievals.length);
}
return null;
}
function bodyload() {
var cookieset = checkcookie();
if (cookieset != null) {
snowStorm.stop(true)
} else {
document.write("<input type='button' value='Stop Snow' onClick='snowStorm.stop()' />");

and add this:


<body onload="bodyload()">


P.S. What is the website?

Cheeseweasel
12-07-2007, 11:47 PM
Do I need to make any amendments to that code? Will inserting it create the button? Where in the code do I insert it?

Thanks, but please answer the questions above as I've no clue how to use it :confused:

Thanks
Cheeseweasel

P.S. The site is www.midnafanforum.com

jscheuer1
12-08-2007, 07:05 AM
The term valid is misleading. HTML elements aren't technically valid for browsers. They are either supported or not. The term valid applies to proper use of the HTML language for a given DOCTYPE.

The onload event that is being recommended here would most likely need to be integrated with the existing one (if any) that starts the snow. If it fires before the snow starts or conflicts with the snow's onload (if any), it won't be of much use. It would be better to simply not start the snow in the first place if the cookie for no snow is present.

However that is worked out, the button to stop the snow shouldn't even be displayed if there is no snow, but perhaps one to start it should be. Likewise, when a person hits the stop button, it should perhaps change into a start button.

To work out all of these subtleties, a link to the existing page would be best:

Please post a link to the page on your site that contains the problematic code so we can check it out.

Cheeseweasel
12-08-2007, 10:38 AM
www.midnafanforum.com is the site.

Master_script_maker
12-08-2007, 01:00 PM
1. upload this changed snowstorm.js to your website
2. change this:
<script type="text/javascript" src="http://ugoi.kg13.com/snowstorm.js"></script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> to this:
<script type="text/javascript" src="http://www.midnafanforum.com/snowstorm.js"></script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. replace:
<a href="javascript:snowStorm.stop()"><img src="http://midnafanforum.com/Themes/safgrey105/images/english/stopsnow.gif" alt="Oekaki" style="margin: 2px 0;" border="0" /></a> with:
<script type="text/Javascript">
function bodyload() {
var cookieset = checkcookie();
if (cookieset != null) {
snowstorm.stop(true)
} else {
document.write('<a href="javascript:snowStorm.stop()"><img src="http://midnafanforum.com/Themes/safgrey105/images/english/stopsnow.gif" alt="Oekaki" style="margin: 2px 0;" border="0" /></a> ');
</script>
4. replace :
<body>
with:
<body onLoad="bodyload()">

Master_script_maker
12-08-2007, 01:07 PM
i already awsered this in your other thread here (http://http://www.dynamicdrive.com/forums/showthread.php?t=27301)
Edit: Deleted

jscheuer1
12-08-2007, 04:10 PM
Is there any reason why you have the script tag here (or included here):


<script type="text/javascript" src="http://ugoi.kg13.com/snowstorm.js"></script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<meta h . . .

If not, it would be better to put it in the head section, just before the closing </head> tag.

Now, most of what Master_script_maker was telling you is probably OK, I don't really know, because I didn't test it. I'm not going to worry about that, instead here is what I would do. I would add the cookie functions from quirksmode (http://www.quirksmode.org/js/cookies.html) to the end of snowstorm.js file:


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);
}

That's because I see no reason to rewrite them or to clog up the rest of the code with cookie code functions or parts of them.

You already have a stop snow button or whatever you want to call it:


<a href="javascript:snowStorm.stop()"><img src="http://midnafanforum.com/Themes/safgrey105/images/english/stopsnow.gif" alt="Oekaki" style="margin: 2px 0;" border="0" /></a>

But since we are going to ask it to do more than just stop the snow, we will need to change it a bit. Here's my idea, get rid of the link (the <a . . . > and </a> tags) around it, and attach the action to the image onclick:


<img src="http://midnafanforum.com/Themes/safgrey105/images/english/stopsnow.gif" alt="Oekaki"
style="border:none;cursor:pointer;margin: 2px 0;"
id="snow_control"
title="Stop Snow"
onclick="my_snow_stop(this);}">

Here's the my_snow_stop function:


function my_snow_stop(el){
if (el.title=='Stop Snow'){
el.title='Start Snow';
el.src='http://midnafanforum.com/Themes/safgrey105/images/english/startsnow.gif';
snowStorm.stop();
createCookie('snow_control',1,5); //5 days persistence
}
else{
eraseCookie('snow_control');
el.title='Stop Snow';
el.src='http://midnafanforum.com/Themes/safgrey105/images/english/stopsnow.gif';
snowStormInit();
}
}

Put it at the end of the script, after the cookie code we already added. You will need to make up a startsnow.gif to go with this. And a bit of code will now need to be added to the init function which currently looks like so:


function snowStormInit() {
setTimeout("snowStorm = new SnowStorm()",500);
}

Replace the existing one with:


function snowStormInit() {
if(!readCookie('snow_control'))
setTimeout("snowStorm = new SnowStorm()",500);
else {
var el;
if(el=document.getElementById('snow_control')){
el.title='Start Snow';
el.src='http://midnafanforum.com/Themes/safgrey105/images/english/startsnow.gif';
}
}
}

Master_script_maker
12-08-2007, 05:04 PM
Now, most of what Master_script_maker was telling you is probably OK, I don't really know, because I didn't test it. I'm not going to worry about that, instead here is what I would do. I would add the cookie functions from quirksmode (http://www.quirksmode.org/js/cookies.html) to the end of snowstorm.js file

i took the quirksmode cookie script and modified it to Cheeseweasel's needs

jscheuer1
12-08-2007, 07:17 PM
i took the quirksmode cookie script and modified it to Cheeseweasel's needs

Good for you! Incidentally, I haven't fully tested my code either, just key portions of it. The reason I used the full cookie code from quirksmode is as I stated:


I see no reason to rewrite them [sic: the functions] or to clog up the rest of the code with cookie code functions or parts of them

Also, since I didn't test your code, the only problem I have with it is that it doesn't appear to address this from the point of view of not initializing the script if the cookie is present, or the idea of providing a start button in place of the stop button if the cookie is already set, or the user hits stop.

At least I didn't see that in your original solution, I haven't looked at all of your subsequent code.

It really doesn't matter to me what the OP chooses to use, as long as it does whatever is required.

Since these are PHP pages with login, it might be even better to set the cookie server side.

Also, if javascript is used, the whole thing could be better integrated into the existing script, making all variables and functions properties of the original script object, as it is written at least well enough to follow that model for the most part. But, I'm just shooting for making it work, and not be too poorly written or thought out.