I’m developing a WordPress theme that allows users to toggle the visibility of the sidebars using Javascript in the same way that wikis allow users to show or hide a table of contents.
When a user clicks the link to hide a sidebar I have a toggle() javascript toggle function that sets the visibility of the sidebar to “none” and also sets a document cookie so that the next time the users visits that page, the sidebar will remember the last toggle state.
I have another javascript function setToggleFromCookie() that gets the cookie and sets the visibility based on that cookie. This all works except:
If a sidebar is hidden (i.e. visibility of the sidebar div = “none” and the document cookie hidesidebar = 1), when the page is loaded, the sidebar appears briefly before the javascript overrides the visibility = “block” with visibility = “none.”
I have put setToggleFromCookie() in jQuery(document).ready(function(), but the page still seems to load and display the sidebar BEFORE setToggleFromCookie() jumps in to hide it…
While annoying, your plugin is functioning properly. The page completely renders itself first, showing the sidebar, and then the JavaScript code activates to hide the sidebar.
The fix for this is to have your PHP code check/grab the user’s cookie and set the display’s sidebar status accordingly. That way when the page renders it will be set to none initially, and then any future image toggles can hide/show the sidebar dynamically.
When the page is loaded again for whatever reason, the same PHP loading code will run again, and set the initial state of the sidebar accordingly, without any need to “follow up” after the initial page load to show/hide the sidebar.
Hopefully this makes sense. If you need some specifics, you might try updating your question with the code involved.