• 0

Getting JavaScript Error Message In IE


Question

I have this javascript code,

function loadState() {
var state = $.cookie('toggleState');
$('div.toggle').each(function(i) {
$(this).toggle(state[i] == 'Y');
});
}

But im getting an error message in Internet Explorer "state is null or not an object"

on this line,

$(this).toggle(state[i] == 'Y');

How do I fix this?

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

You're assuming that a cookie called "toggleState" already exists. You need to check to see if state is not a null string or empty, and also ensure that the string is the correct length...

function loadState() {
    var state = $.cookie('toggleState');

    $('div.toggle').each(function(i) {
        if(state && state.length >= +i + 1) {
            $(this).toggle(state[i] == 'Y');
        } else {
            //Set default toggle state here if we can't find the state for this toggle.
        }

    });
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.