• 0

Getting JavaScript Error Message In IE


Question

jrolson    49

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 post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
+Majesticmerc    872

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 post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.