4

A simple page, with the button:

<button id="MyButton">Hello</button>

The problem is:

  • I need the button to show if Javascript is disabled
  • I need the button to be hidden if Javascript is enabled

A simple Jquery:

$('#MyButton').hide();

Isn't sufficient as the button 'flickers', it loads and then hides itself and it's very noticeable.

The one way I've found to make this work is by using JS to hide it immediately after:

<button id="MyButton">Hello</button>
<script>
    document.getElementById('MyButton').style.visibility = 'hidden';
</script>

This seems a bit ugly and breaking a couple of good standards rules, is this the only way to hide the button without a flickering effect?

8
  • 7
    <noscript> is the key Commented May 2, 2012 at 15:10
  • Can't you just add 'style="display:none;"'? Commented May 2, 2012 at 15:11
  • @gdoron DOH! Missing the obvious! That's amazing thank you, please put it in an answer if you want some points! Commented May 2, 2012 at 15:11
  • @TomGullen, the answer format. thanks Commented May 2, 2012 at 15:12
  • @andreapier if JS is disabled how do you get it to then show? Commented May 2, 2012 at 15:12

4 Answers 4

14

Sounds like a job for the noscript tag!

<noscript>
    <button id="MyButton">Hello</button>
</noscript>
Sign up to request clarification or add additional context in comments.

Comments

10
<noscript>
    <button id="MyButton">Hello</button>
</noscript>

The HTML NoScript Element () defines a section of html to be inserted if a script type on the page is unsupported or if scripting is currently turned off in the browser.

MDN

Comments

3

Use <noscript> element which holds a <style> element. <noscript> won't be parsed by browsers with active JavaScript. So that's:

<style>#MyButton { display: none; }</style>
<noscript><style> button#MyButton { display: inline; }</style></noscript>

Or better yet:

<noscript><button id="MyButton">Button</button></noscript>

Note I'm using button#MyButton to have a higher specificity value so that it will override the first method.

Comments

2

use a no-js class:

<button id="MyButton" class="no-js">Hello</button>

define that class to hide the element:

.no-js { display: none; }

Then, in your ready handler, remove the no-js class - it will stay there if javascript is not enabled.

6 Comments

Again, will cause flicker. The button won't appear and then suddenly it will.
@Truth. If the js doesn't wait for DOM ready there won't be flickering. but <noscript> does it all for you out of the box, so why bother?
Which is the exact reason for my downvote. Apparently, 4 other people don't think the same.
I'll have to keep my eye open looking for that, but I haven't encountered that behavior yet. If you wait for document ready, probably a little more likely, but DOM ready? Haven't seen it yet.
@kinakuta. DOM ready == document ready. Did you mean document load? anyway it happens and happens a lot.
|

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.