Why won't my function work?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
dissonantallure
Forum Newbie
Posts: 21
Joined: Tue Feb 03, 2009 7:48 pm

Why won't my function work?

Post by dissonantallure »

I have a function that shows or hides a div, for example;

Code: Select all

// Function to show div
function SCdiv(divid){
    if(document.getElementById(divid).style.display == 'none')
       document.getElementById(divid).style.display = 'block';
    }  
and to use this function;

Code: Select all

<script type="text/javascript">
window.onload=CheckCookies;
</script>
<form name="SC" method="post" action="#">
<input type="checkbox" name="test" id="test" value="Other"  onclick="SCdiv('Other');" />
<div id="Other" style="display:none">
<input type="text" name="other"/>
</div>
Everything works perfect. The problem is when i try to add this code which is a cookie that is supposed to save the state of checkboxes in my form.

Code: Select all

//This Function Creates your Cookie for you just pass in the Cookie Name, Value, and number of days before you want it to expire.
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=/";
}
 
//This Function reads the value of a given cookie for you.  Just pass in the cookie name and it will return the value.
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;
}
 
//This Function Erases Cookies.  Just pass in the name of the cookies you want erased.
function EraseCookie(name)
{
    CreateCookie(name,"",-1);
}
 
//Sets or UnSets Cookies for given checkbox after it's been clicked on/off.
function ChangeBox(CheckBox)
{
    if (document.getElementById(CheckBox).checked)
    {
        var CurrentCookie = ReadCookie("Scookie");
        CurrentCookie = CurrentCookie + CheckBox;   
        CreateCookie("Scookie",CurrentCookie,"100");
    }
    else
    {
        var CurrentCookie = ReadCookie("Scookie");
        CurrentCookie = CurrentCookie.replace(CheckBox,""); 
        CreateCookie("Scookie",CurrentCookie,"100");
    }
}
 
//Runs on body load to check history of checkboxes on the page.
function CheckCookies()
{
 
    var CurrentCookie = ReadCookie("Scookie");
 
    for (i=0; i<document.SC.elements.length; i++)
    {
        if (document.SC.elements[i].type == "checkbox")
        {
            document.SC.elements[i].onclick = function() {ChangeBox(this.id);};
            if (CurrentCookie && CurrentCookie.indexOf(document.SC.elements[i].id) > -1)
            {
                document.SC.elements[i].checked = true;
            }
        }
    }
}
 
//Clears Form
function ClearBoxes()
{
    for (i=0; i<document.SC.elements.length; i++)
    {
        if (document.SC.elements[i].type == "checkbox")
        {
            document.SC.elements[i].checked = false;
            ChangeBox(document.SC.elements[i].id);
        }
    }
}
When I add the cookie functions my div no longer displays it just remains hidden. Why isn't my code working?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Why won't my function work?

Post by JAB Creations »

I stopped using JavaScript to set cookies because Safari refuses to allow it so I strictly stick to generating cookies with PHP.

Are you looking at the JavaScript/Error console in Firefox/IE8/Opera/Safari?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Why won't my function work?

Post by kaszu »

Your code works for me, just make sure you have CheckCookies declared before following code, not after it

Code: Select all

window.onload=CheckCookies;
because Safari refuses to allow it
That's not true, at least for Safari 3 & 4. Only browser with which I had problems with javascript cookies was IE6
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Why won't my function work?

Post by JAB Creations »

Then it was probably Safari 2 then. There are unfortunately some people who don't comprehend that if they own a Mac and don't upgrade to newer versions that they should download a free browser. I still highly recommend setting cookies only via serverside, less code to worry about.
Post Reply