Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 05-04-2009, 01:13 PM
Mazinger's Avatar
Mazinger Mazinger is offline
 
Join Date: Nov 2006
Location: Egypt
Posts: 355
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Any body here experienced with JS and knows how vBulletin handle cookies?

Please, it's urgent.
Reply With Quote
  #3  
Old 05-04-2009, 02:21 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Have you looked at the API? vBulletin API
Reply With Quote
  #4  
Old 05-04-2009, 03:18 PM
Mazinger's Avatar
Mazinger Mazinger is offline
 
Join Date: Nov 2006
Location: Egypt
Posts: 355
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm sure I'll sink if I looked at this again.

It seems that the JS code I posted does already have a cookie script into it, it doesn't work for a reason though.
Reply With Quote
  #5  
Old 05-12-2009, 10:26 PM
Lea Verou Lea Verou is offline
 
Join Date: Jul 2005
Location: Greece
Posts: 1,856
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Is the problem that the cookie is not saved correctly or that it's not read correctly? Check your cookies to find out. It would help us go through less code if we know the exact problem.
By the way, most of these functions are already present in vBulletin's JS code or the YUI modules it uses. I'd suggest you take a look, duplicating functionality is not good, since your users download more stuff = higher waiting time for the page to load.
Reply With Quote
  #6  
Old 05-12-2009, 10:36 PM
RLShare RLShare is offline
 
Join Date: Jun 2008
Posts: 499
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

If you are attatching your function to the window.onload event in your head or a file included in the head then it is more then likely getting replaced by the opening body tag of the page in VB.
Reply With Quote
  #7  
Old 05-14-2009, 08:27 AM
Mazinger's Avatar
Mazinger Mazinger is offline
 
Join Date: Nov 2006
Location: Egypt
Posts: 355
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks everybody, I've already handled this issue with an alternate JS code I got from somewhere.

I appreciate your help though.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:01 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04133 seconds
  • Memory Usage 2,230KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • 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
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete