Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9038873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:35:41+00:00 2026-06-16T09:35:41+00:00

I have never used cookies before, so I am using a peice of code

  • 0

I have never used cookies before, so I am using a peice of code I am very unfamiliar with.

It was working all fine, until I noticed just now that for select boxes, it is not working for any values after the tenth index. (for index 10 and above).

I have looked at the cookie stored on my system, and it appears t be saving them correctly. (I saw select10) ETC stored properly.

When it runs onload of body however, it is not loading in the values properly.

Here is the cookie code I am using:

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

var expDays = 100;
var exp = new Date(); 

exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {  
    var endstr = document.cookie.indexOf (";", offset);  
    if (endstr == -1) { endstr = document.cookie.length; }
    return unescape(document.cookie.substring(offset, endstr));
}



function GetCookie (name) {  
    var arg = name + "=";  
    var alen = arg.length;  
    var clen = document.cookie.length;  
    var i = 0;  

    while (i < clen) {    
        var j = i + alen;    
        if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
        i = document.cookie.indexOf(" ", i) + 1;    
        if (i == 0) break;   
    }  

    return null;
}



function SetCookie (name, value) {  
    var argv = SetCookie.arguments;  
    var argc = SetCookie.arguments.length;  
    var expires = (argc > 2) ? argv[2] : null;  
    var path = (argc > 3) ? argv[3] : null;  
    var domain = (argc > 4) ? argv[4] : null;  
    var secure = (argc > 5) ? argv[5] : false;  

    document.cookie = name + "=" + escape (value) + 
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
    ((path == null) ? "" : ("; path=" + path)) +  
    ((domain == null) ? "" : ("; domain=" + domain)) +    
    ((secure == true) ? "; secure" : "");
}

// use the following code to call it:
//  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">

function cookieForms() {  
    var mode = cookieForms.arguments[0];

    for(f=1; f<cookieForms.arguments.length; f++) {
        formName = cookieForms.arguments[f];

        if(mode == 'open') {    
            cookieValue = GetCookie('saved_'+formName);

            if(cookieValue != null) {
                var cookieArray = cookieValue.split('#cf#');

                if(cookieArray.length == document[formName].elements.length) {
                    for(i=0; i<document[formName].elements.length; i++) {
                        if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
                        else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
                        else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
                        else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
                    }
                }
            }
        }

        if(mode == 'save') {    
            cookieValue = '';

            for(i=0; i<document[formName].elements.length; i++) {
                fieldType = document[formName].elements[i].type;

                if(fieldType == 'password') { passValue = ''; }
                else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
                else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
                else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
                else { passValue = document[formName].elements[i].value; }
                cookieValue = cookieValue + passValue + '#cf#';
            }
            cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
            SetCookie('saved_'+formName, cookieValue, exp);     
        }   
    }
}

//  End -->
</script>

I beleive the problem lies with the following line, found about 3/4 of the way down the code block above (line 68):

if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }

Just for reference, here is the opening body tag I am using:

<body style="text-align:center;" onload="cookieForms('open', 'ramsform', 'decksform', 'hullsform', 'crewform', 'shipwrightform'); Swap(crew0z,'crew0i'); Swap(crew1z,'crew1i'); Swap(crew2z,'crew2i'); Swap(crew3z,'crew3i'); Swap(crew4z,'crew4i'); Swap(crew5z,'crew5i'); Swap(crew6z,'crew6i'); Swap(crew7z,'crew7i'); Swap(crew8z,'crew8i'); Swap(crew9z,'crew9i');" onunload="cookieForms('save', 'ramsform', 'decksform', 'hullsform', 'crewform', 'shipwrightform');">

(Please ignore the swap()’s as they are unrelated)

Page I am working on can be found: http://webhostlet.com/POP.htm

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:35:43+00:00Added an answer on June 16, 2026 at 9:35 am

    In both the open and save codes, change:

    document[formName].elements[i].options.selectedIndex
    

    to:

    document[formName].elements[i].selectedIndex
    

    options is an array of all the options, the selectedIndex property belongs to the select element that contains them.

    Change:

    cookieArray[i].substring(7, cookieArray[i].length-1)
    

    to:

    cookieArray[i].substring(6)
    

    You were off by 1 because you forgot that it’s 0-based counting. The second argument isn’t needed, it defaults to the rest of the string.

    The reason it worked for the first 10 menu items is a quirk of substring: if the second argument is lower than the first, it swaps them! So "select5".substring(7, 6) is treated as "select5".substring(6, 7), which gets the last character of the string. But for the longer strings, it was `"select35".substring(7, 7), which is an empty string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never used namespaces for my code before. (Other than for using STL
I have never used Foxpro before. Can you convert the following Foxpro code into
I have never used Try-catch in my code before, but now I need to
I have used sessions before but never cookies. I would like to use cookies
I have NEVER used ANY version control system before. I am currently using the
I have never used Prototype before. But now when I'm using Rails, it seems
I have never used DBIx::Class until today, so I'm completely new at it. I'm
I have never used array_combine before and I am getting a Boolean instead of
Just trying to understand that - I have never used it before. How is
I have never really used regular expressions all that much and as such i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.