I’m reworking an old website and am focusing on making the Javascript/jQuery as unobtrusive as possible. I’m looking for some advice on one part of the project: UJ requires that, if Javascript is turned off in a browser, all the site content is still available to the user. One part of my site has a large list of items. Each item in the list has it’s own description, which uses CSS display:none to hide it by default. Clicking on the item toggles the visibility of the description using jQuery .toggle():, so a person unable to use Javascript (for whatever reason) would never see it. If I use Javascript/jQuery rather than CSS to hide the descriptions, they are all momentarily visible when the page loads, which I want to avoid. So what’s the best solution?
I’m reworking an old website and am focusing on making the Javascript/jQuery as unobtrusive
Share
basically you can insert a class “no-js” in your
htmlelement, like soand if javascript is enabled on the client you soon remove that class (using modernizr or a simpler code snippet like
in this way you can avoid the FOUC (flash of unstyled content) simply using
.no-jsand.jsclass in the css rules like so:this approach is also used by H5BP
Note that you don’t really need to prepend
.no-jsclass to each rule: thinking in terms of “progressive enhancement” and “unobtrusive javascript” you will code and style first for a page that also works without javascript, then you will add functionality (and style specificity, prepending the.jsclass)