Results 1 to 4 of 4

Thread: [RESOLVED] Javascript read cookie

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Resolved [RESOLVED] Javascript read cookie

    i have this js file that i use as part of a drop down menu.

    Code:
    var anylinkmenu1={divclass:'anylinkmenu', inlinestyle:'', linktarget:''} 
    
    anylinkmenu1.items=[
    	["Home", "../index.php"], 
    	["Logout", "../process.php"]//no comma following last entry!
    ]
    
    
    ]

    what i want to do is read a cookie that already exist called siteid then append the menu caption "Home" with the value in the cookie. i have tried starting the file with this
    var siteid = get_cookie ( "siteid" );

    then placing the siteid variable in the ["Home", "../index.php"],
    but with no luck.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: Javascript read cookie

    I got it to work. what i did was to add this at the start of the file to read the cookie and remove any + signs

    Code:
    function cookie_read(c_name) 
    { 
    var comp = c_name + "="; 
    var cooks = document.cookie.split(';'); 
    for(var i=0;i < cooks.length;i++) 
    { 
    var check = cooks[i]; 
    while (check.charAt(0) == " ") 
    check = check.substring(1,check.length);
    if (check.indexOf(comp) == 0) 
    return check.substring(comp.length,check.length); 
    } 
    return null; 
    } 
    
    UnitChangedTo = cookie_read('UnitChangedTo')
    UnitChangedTo = UnitChangedTo.replace("+"," ");
    then in the menu code

    Code:
    [UnitChangedTo, "../GetUnitNumber.php"],
    Last edited by seanwpb; Nov 19th, 2010 at 06:16 AM. Reason: typo

  3. #3
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [RESOLVED] Javascript read cookie

    This is the Java forum not the JavaScript forum, they are two completely different languages - a common mistake.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Javascript read cookie

    Thread moved

Posting Permissions

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



Click Here to Expand Forum to Full Width