I just thought I'd post a solution I came up with that may be helpful
to anyone using the treeview plugin for navigation.

The problem I ran into was that the cookie persistence was working,
but since I was using the tree for navigation to different directories
of my site, sometimes the cookie wouldn't get set correctly and the
state of the tree would end up being one step behind what the user had
just clicked -- so the previously clicked node would be expanded
rather than the current one.

Thanks to some helpful posts I found on here, I discovered that the
problem was the cookie path.

The solution is to use an (undocumented) option that was added to the
plugin as of version 1.4: 'cookieOptions', which lets you set the
cookie path.

If your navigation tree could potentially be used for any page in your
domain, then you would set the cookie path to the root, like this:

cookieOptions: {path: '/'}

..or if your site is in a subdirectory you should use this:
cookieOptions: {path: '/my_subdirectory/'}

In my case I'm using PHP and I have my site root stored in a constant
called WEB_ROOT, so my full code was:

        $(document).ready(function() {
                $("#sideNav ul").treeview({
                        collapsed: true,
                        unique: true,
                        persist: "cookie",
                        cookieOptions: {path: '<?= WEB_ROOT ?>'}
                });
        });

Hope this is helpful to somebody.

Reply via email to