View Single Post
  #1  
Old 05-02-2009, 11:56 AM
Mazinger's Avatar
Mazinger Mazinger is offline
 
Join Date: Nov 2006
Location: Egypt
Posts: 355
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default How to save custom options with cookies

Say, I have implemented a JS code into my forum that allows the user to expand/reduce the forum divider width; it works in the currently page, but when moving to any other page, the browser forgets about the selected options.

I want the browser to remember it and deliver it every time with cookies.

Does any one know how to do that?

Thanks in advance.

--------------- Added [DATE]1241339504[/DATE] at [TIME]1241339504[/TIME] ---------------

For example, this JS code. It doesn't save choices with cookies with my 3.7.4 forum:

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



function getElementsByClassName(className, tag, elm){
    var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
    var tag = tag || "*";
    var elm = elm || document;
    var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
    var returnElements = [];
    var current;
    var length = elements.length;
    for(var i=0; i<length; i++){
        current = elements[i];
        if(testClass.test(current.className)){
            returnElements.push(current);
        }
    }
    return returnElements;
}



//*** This code is copyright 2003 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code satisfies the conditions.)


//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);

function AttachEvent(obj,evt,fnc,useCapture){
    if (!useCapture) useCapture=false;
    if (obj.addEventListener){
        obj.addEventListener(evt,fnc,useCapture);
        return true;
    } else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
    else{
        MyAttachEvent(obj,evt,fnc);
        obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
    }
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
    if (!obj.myEvents) obj.myEvents={};
    if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
    var evts = obj.myEvents[evt];
    evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
    if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
    var evts = obj.myEvents[evt];
    for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

AttachEvent(window,'load',loadPageState,false);

function loadPageState()
{
    // inject search here text + assign listener to remove it
    var searchInput = document.getElementById('searchquery');
    if(searchInput != null)
    {
        searchInput.value = "Search here";
        AttachEvent(searchInput,'click',removeSearchText,false);
    }    
    
    if(readCookie('fluid') == null)
    {
        createCookie('fluid',0,9999);    
    }
    
    if(readCookie('podmenu') == null)
    {
        createCookie('podmenu',0,9999);    
    }
    
    var fluid = readCookie('fluid');
    var podmenu = readCookie('podmenu');
    
    /*if(fluid != null && podmenu != null)
    {
        var ourBody = document.body;
        if(fluid == 0 && podmenu == 1)
        {
            ourBody.setAttribute('class', 'max-fixed');
        }
        else if(fluid == 1 && podmenu == 0)
        {
            ourBody.setAttribute('class', 'fluid-hidden');
        }
        else if(fluid == 1 && podmenu == 1)
        {
            ourBody.setAttribute('class', 'fluid-visible');
        }
        else
        {
            ourBody.setAttribute('class', 'min-fixed');
        }            
    }*/
    
    // assign our listeners to the controls
    var ffControl = document.getElementById('layout-toggle');
    if(ffControl != null)
    {
        AttachEvent(ffControl,'click',toggleFF,false);    
    }
    
    var pmControl = document.getElementById('content-toggle');    
    if(ffControl != null)
    {
        AttachEvent(pmControl,'click',togglePM,false);    
    }
    
    // assign hover event handlers
    var hovers = getElementsByClassName("hover", "table", document);
    for(var i = 0; i < hovers.length; i++)
    {
        hovers[i].onmouseover = doHover1;
        hovers[i].onmouseout = doHover2;
    }
}

function doHover1()
{
    //this.className = 'hover2';
}

function doHover2()
{
    //this.className = 'hover';
}

function removeSearchText()
{
    var searchInput = document.getElementById('searchquery');
    if(searchInput != null)
    {
        if(searchInput.value == 'Search here')
        {
            searchInput.value = "";
        }
    }
    //searchInput.class =     
}

function toggleFF()
{
    var fluid = readCookie('fluid');
    var podmenu = readCookie('podmenu');
    var ourBody = document.getElementById("resizer");
    
    
    if(fluid != null && podmenu != null && ourBody != null)
    {
        if(fluid == 1)
        {
            createCookie('fluid', 0, 9999);
            if(podmenu == 1)
            {
                ourBody.setAttribute('class', 'max-fixed');
                ourBody.className = 'max-fixed';
            }
            else
            {
                ourBody.setAttribute('class', 'min-fixed');
                ourBody.className = 'min-fixed';
            }
            
        }
        else
        {
            createCookie('fluid', 1, 9999);
            if(podmenu == 1)
            {
                ourBody.setAttribute('class', 'fluid-visible');
                ourBody.className = 'fluid-visible';
            }
            else
            {
                
                ourBody.setAttribute('class', 'fluid-hiden');
                ourBody.className = 'fluid-hidden';    
            }
        }
    }
    return false;
}

function togglePM()
{
    var fluid = readCookie('fluid');
    var podmenu = readCookie('podmenu');
    var ourBody = document.getElementById("resizer");

    if(fluid != null && podmenu != null && ourBody != null)
    {
        if(podmenu == 1)
        {
            createCookie('podmenu', 0, 9999);
            if(fluid == 1)
            {
                ourBody.setAttribute('class', 'fluid-hidden');
                ourBody.className = 'fluid-hidden';                
            }
            else
            {
                ourBody.setAttribute('class', 'min-fixed');
                ourBody.className = 'min-fixed';                    
            }
            
        }
        else
        {
            createCookie('podmenu', 1, 9999);
            if(fluid == 1)
            {
                ourBody.setAttribute('class', 'fluid-visible');
                ourBody.className = 'fluid-visible';
            }
            else
            {
                ourBody.setAttribute('class', 'max-fixed');
                ourBody.className = 'max-fixed';
            }
        }
    }
    return false;
}
Reply With Quote
 
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01097 seconds
  • Memory Usage 1,812KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD_SHOWPOST
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • reputationlevel
  • showthread
Included Files:
  • ./showpost.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showpost_start
  • bbcode_fetch_tags
  • bbcode_create
  • postbit_factory
  • showpost_post
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • showpost_complete