Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Memory Code

  1. #1
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Memory Code

    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:

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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    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

    Edit: .

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

  4. #4
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  5. #5
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    try this:
    Code:
    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:
    Code:
    <body onload="bodyload()">
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  6. #6
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks - only problem is that I only want that to happen when a certain button is pressed. How could I do that?

  7. #7
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A Very Simple Cookie Request

    (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

  8. #8
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    use this (valid netscape/firefox ver 6.2 and ie ver 4.0):
    Code:
    <button onclick="snowStorm.stop()">Stop Snow</button>
    or this (valid netscape/firefox ver 3.0 and ie ver 3.0):
    Code:
    <input type="button" value="Stop Snow" onClick="snowStorm.stop()" />
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  9. #9
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    try this:
    Code:
    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:
    Code:
    <body onload="bodyload()">
    P.S. What is the website?
    Last edited by Master_script_maker; 12-07-2007 at 11:17 PM.
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  10. #10
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

    Thanks
    Cheeseweasel

    P.S. The site is www.midnafanforum.com
    Last edited by Cheeseweasel; 12-07-2007 at 11:50 PM. Reason: site url

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •