5

I want to display to the user a message that says "please enable JavaScript" in the case that JavaScript is disabled. I want that message, and nothing else to be displayed when JavaScript is disabled.

So, to do this, I can put the message in the DOM and hide all other elements with display:none. Then in JavaScript I can set the message to display hidden and show all other elements.

But I get a flicker with this approach. The error message shows up for a while (especially on mobile browsers) before it gets hidden.

How can I minimize the time the error message is displayed for?

1
  • btw, the behavior you are seeing is often referred to as a flash of unstyled content -- "fouc" might help your googling Commented Mar 17, 2011 at 2:49

6 Answers 6

15

You're looking for the <noscript> tag.

Sign up to request clarification or add additional context in comments.

14 Comments

That's ideal, but won't solve the problem of the flicker on a Nokia phone.
I take that back...that could work. Error message in NOSCRIPT, then your main content in the hidden div that you then show via javascript. The flicker will be there from blank to content, but that's probably better than a flicker from error to content. (slow Nokia devices may still see the content flicker off, but if you make the NOSCRIPT area tall enough, that'd be off-screen)
<noscript> should be avoided. The proper way to do it is to use display:none; display:block; as i stated in my answer. You have 123k reputation and recommending <noscript> ??
@Hussein: This is how to prevent flicker. You could also hide the parent element right after it starts, before its content.
@Hussein: There is no need to be rude. First you say that @SLaks shouldn't recommend noscript because he has 123k reputation. Now you accuse me of "not getting" what you said above while in fact you didn't say anything to support your irrational fear of the noscript tag. These are not reasonable arguments. Just because there are other ways to emulate the behavior of some tag doesn't mean that it shouldn't be used. Judging from the votes on the answers the majority of people seem to agree. So does the W3C's HTML Working Group
|
3

Inline javascript has to run before the rest of the document is loaded. You can use this behaviour to add style to the page to hide the desired element, before the element is loaded. This should theoretically stop FOUC across all and every browser (including mobile). Here's an example of a standalone HTML that shows a message to those with no javascript. This technique also takes care of what Hussein was mentioning about Firewalls blocking javascript:

<!doctype html>
<html>
<head>
<title>No FOUC</title>
<script type="text/javascript">
document.write('<style type="text/css">#no-js { display: none; }</style>');
</script>
</head>
<body>
<div id="no-js">You don't have javascript, BOOOO</div>
</body>
</html>

Comments

0

You can do this. If JavaScript is disabled it will show the message "JavaScript Disabled"

 <p id="jsDisabled">JavaScript disabled.</p>

<script type="text/javascript">
    function hideScriptDisabled() {
        document.getElementById("jsDisabled").display = "none";
    }
    hideScriptDisabled();
    </script>

If that is creating a flickering problem, use something like this,

document.write(".jsDisabled { display: none; }");

3 Comments

You have just reinvented <noscript>
The noscript tag only detects whether the browser has JavaScript enabled or not. If JavaScript is disabled in the Firewall rather than in the browser then the JavaScript will not run and the content of the noscript tag will not be displayed. It's better to let javascript code overwrite with a javascript enabled page with simple display:block;, display:none;.
Hussein's comment seems to be copied from javascript.about.com/od/reference/a/noscriptnomore.htm. Interestingly, if you check the meta tags you see that this article was once called "5 Remarkably Good Reasons to Avoid &#39;Noscript&#39; and Use Java".
-1

See 1 Way To Avoid the Flash of Unstyled Content

Comments

-1

To avoid javascript disable problem.

before starting any writing on your webpage just put the below code.

<!-- saved from url=(0014)about:internet-->

You have to never ask any question to end user.

First try it.

Comments

-3

You really can't. The flicker is just the way it is...especially on slow mobile devices (namely Nokia...OH HOW I HATE NOKIA!)

The only other option is to load a splash page first. In the HEAD add a meta refresh of several seconds that will refresh to a 'turn on javascript error'. On the BODY, add a javascript redirect that should trigger immediately.

You will still have a flash, and of course, one more page request to the server. But it may be a 'prettier' flash.

4 Comments

why the downvote ? really ? would you use the same method for your own site? when you clearly have better solution ( as shown above ).
Not sure what you feel is 'clearly better'. Noscript is fine at showing content to those without JS enabled, but does nothing to hide the rest of the content. So you have to hide it by default and then show it via JS, hence the flicker on slow connections/devices.
google.com/support/webmasters/bin/answer.py?answer=35769 "•Avoid hidden text or hidden links." you SHOULD NOT, display diffrent content. but a nice explanation why some things do not work, would be pretty nice.
I completely agree with you in that what the OP is asking for is not practical nor recommended. However, bosses are known to ask for things that are not practical nor recommended. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.