I have some JQuery code that shows or hides a div.
$("div#extraControls").show(); // OR .hide()
I initially want the div to be not visible so I used:
$(document).ready(function() {
$("div#extraControls").hide();
});
However, on the browser, the content loads visible for a second before disappearing, which is not what I want.
How do I set the hide the element before the page loads whilst keeping the ability to show hide it dynamically with a script?
The best way to load javascript function over page is loaded, is
In your case
What you could do is insert a inline
script
tag at the end of the document, or immediately after the div with id "extraControls". Should be fire a little sooner than.Of course you could do this server-side as well, but maybe there's a good reason you don't want to do that (like, have the controls visible if the browser does NOT support javascript).
Make a css class
Add this to any element you don't want to be visible when loading the page. Then use
$("div#extraControls").show();
to display it again.With CSS3 you can actually hide content until a page loads by taking advantage of the
:only-child
selector.Here is a demonstration of how to do this. The basic idea is that CSS while loading will add a single node to the DOM while loading that node and its child nodes. Further, once a second child is added to the given parent node the
:only-child
selector rule is broken. We match against this selector and setdisplay: none
to hide the given node.