• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

Getting JavaScript Error Message In IE

Question

jrolson    45

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?

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
sweetsam    31

Use something else instead of 'state'

Share this post


Link to post
Share on other sites
  • 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.
        }

    });
}

Share this post


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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.