Is there a HTML opposite to <noscript>?
Asked Answered
I

12

129

Is there a tag in HTML that will only display its content if JavaScript is enabled? I know <noscript> works the opposite way around, displaying its HTML content when JavaScript is turned off. But I would like to only display a form on a site if JavaScript is available, telling them why they can't use the form if they don't have it.

The only way I know how to do this is with the document.write(); method in a script tag, and it seems a bit messy for large amounts of HTML.

Inextricable answered 27/8, 2008 at 14:44 Comment(0)
M
63

You could have an invisible div that gets shown via JavaScript when the page loads.

Mathamathe answered 27/8, 2008 at 14:46 Comment(0)
H
248

Easiest way I can think of:

<html>
<head>
    <noscript><style> .jsonly { display: none } </style></noscript>
</head>

<body>
    <p class="jsonly">You are a JavaScript User!</p>
</body>
</html>

No document.write, no scripts, pure CSS.

Hypostasize answered 10/1, 2009 at 19:6 Comment(6)
Not allowed in the xhtml standard at least.Tribadism
@Dykam: it's allowed in HTML5: dev.w3.org/html5/spec/Overview.html#the-noscript-element and in practice, it's supported by virtually every browser.Hypostasize
@Will, Bound to have black sheeps. Isn't the invisible div solution equally clean yet more resilient?Shallot
It's advisable to use display: none !important, to prevent it to be overriden by more specific rules (e.g. by id or css selectors), that are aimed for its use when scripts are enabled.Tepefy
A word of warning: current versions of Chrome don't parse the tags inside a <noscript> the first time you refresh a page after disabling Javascript in the preferences. You need to hit reload twice to get it to work.Crysta
Clean, concise, doesn't use javascript. I like it.Popery
M
63

You could have an invisible div that gets shown via JavaScript when the page loads.

Mathamathe answered 27/8, 2008 at 14:46 Comment(0)
P
9

First of all, always separate content, markup and behaviour!

Now, if you're using the jQuery library (you really should, it makes JavaScript a lot easier), the following code should do:

$(document).ready(function() {
    $("body").addClass("js");
});

This will give you an additional class on the body when JS is enabled. Now, in CSS, you can hide the area when the JS class is not available, and show the area when JS is available.

Alternatively, you can add no-js as the the default class to your body tag, and use this code:

$(document).ready(function() {
    $("body").removeClass("no-js");
    $("body").addClass("js");
});

Remember that it is still displayed if CSS is disabled.

Penrose answered 4/8, 2009 at 19:47 Comment(0)
S
9

I don't really agree with all the answers here about embedding the HTML beforehand and hiding it with CSS until it is again shown with JS. Even w/o JavaScript enabled, that node still exists in the DOM. True, most browsers (even accessibility browsers) will ignore it, but it still exists and there may be odd times when that comes back to bite you.

My preferred method would be to use jQuery to generate the content. If it will be a lot of content, then you can save it as an HTML fragment (just the HTML you will want to show and none of the html, body, head, etc. tags) then use jQuery's ajax functions to load it into the full page.

test.html

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" charset="utf-8">
     $(document).ready(function() {
       $.get('_test.html', function(html) {
         $('p:first').after(html);
       });
     });
    </script>
</head>
<body>
  <p>This is content at the top of the page.</p>
  <p>This is content at the bottom of the page.</p>
</body>
</html>

_test.html

<p>This is from an HTML fragment document</p>

result

<p>This is content at the top of the page.</p>
<p>This is from an HTML fragment document</p>
<p>This is content at the bottom of the page.</p>
Saxony answered 24/8, 2010 at 13:26 Comment(1)
This method makes your pages load slower. If you wanted to do this, you could at least embed the stuff you want to use in the script tag.Proa
V
3

I have a simple and flexible solution, somewhat similar to Will's (but with the added benefit of being valid html):

Give the body element a class of "jsOff". Remove (or replace) this with JavaScript. Have CSS to hide any elements with a class of "jsOnly" with a parent element with a class of "jsOff".

This means that if JavaScript is enabled, the "jsOff" class will be removed from the body. This will mean that elements with a class of "jsOnly" will not have a parent with a class of "jsOff" and so will not match the CSS selector that hides them, thus they will be shown.

If JavaScript is disabled, the "jsOff" class will not be removed from the body. Elements with "jsOnly" will have a parent with "jsOff" and so will match the CSS selector that hides them, thus they will be hidden.

Here's the code:

<html>
    <head>
        <!-- put this in a separate stylesheet -->
        <style type="text/css">
            .jsOff .jsOnly{
                display:none;
            }
        </style>
    </head>

    <body class="jsOff">
        <script type="text/javascript">
            document.body.className = document.body.className.replace('jsOff','jsOn');
        </script>

        <noscript><p>Please enable JavaScript and then refresh the page.</p></noscript>

        <p class="jsOnly">I am only shown if JS is enabled</p>
    </body>
</html>

It's valid html. It is simple. It's flexible.

Just add the "jsOnly" class to any element that you want to only display when JS is enabled.

Please note that the JavaScript that removes the "jsOff" class should be executed as early as possible inside the body tag. It cannot be executed earlier, as the body tag will not be there yet. It should not be executed later as it will mean that elements with the "jsOnly" class may not be visible right away (as they will match the CSS selector that hides them until the "jsOff" class is removed from the body element).

This could also provide a mechanism for js-only styling (e.g. .jsOn .someClass{}) and no-js-only styling (e.g. .jsOff .someOtherClass{}). You could use it to provide an alternative to <noscript>:

.jsOn .noJsOnly{
    display:none;
}
Vervet answered 3/6, 2011 at 10:42 Comment(0)
P
3

In the decade since this question was asked, the HIDDEN attribute was added to HTML. It allows one to directly hide elements without using CSS. As with CSS-based solutions, the element must be un-hidden by script:

<form hidden id=f>
Javascript is on, form is visible.<br>
<button>Click Me</button>
</form>
<script>
document.getElementById('f').hidden=false;
</script>
<noscript>
Javascript is off, but form is hidden, even when CSS is disabled.
</noscript>

Precede answered 27/8, 2019 at 0:30 Comment(2)
Nice one. However, it's important to keep in mind the note listed at the spec, in particular this bit: "Because this attribute is typically implemented using CSS, it's also possible to override it using CSS. For instance, a rule that applies display: block to all elements will cancel the effects of the hidden state."Gambrinus
I would add style="display:none !important" and remove it with .removeAttribute("style") or .style.display = nullActiniform
M
1

You could also use Javascript to load content from another source file and output that. That may be a bit more black box-is than you're looking for though.

Meadors answered 27/8, 2008 at 14:46 Comment(0)
D
1

Here's an example for the hidden div way:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            *[data-when-js-is-on] {
                display: none;
            }
        </style>
        <script>
            document.getElementsByTagName("style")[0].textContent = "";
        </script>
    </head>
    <body>
        <div data-when-js-is-on>
            JS is on.
        </div>
    </body>
</html>

(You'd probably have to tweak it for poor IE, but you get the idea.)

Dumbstruck answered 1/9, 2008 at 11:32 Comment(0)
A
1

My solution

.css:

.js {
display: none;
}

.js:

$(document).ready(function() {
    $(".js").css('display', 'inline');
    $(".no-js").css('display', 'none');
});

.html:

<span class="js">Javascript is enabled</span>
<span class="no-js">Javascript is disabled</span>
Aitchbone answered 18/7, 2013 at 12:58 Comment(1)
In your html example, don't you want to end the lines with </span>?Noeminoesis
V
0

Alex's article springs to mind here, however it's only applicable if you're using ASP.NET - it could be emulated in JavaScript however but again you'd have to use document.write();

Vav answered 27/8, 2008 at 14:47 Comment(0)
P
0

You could set the visibility of a paragraph|div to 'hidden'.

Then in the 'onload' function, you could set the visibility to 'visible'.

Something like:

<body onload="javascript:document.getElementById(rec).style.visibility=visible"> <p style="visibility: visible" id="rec">This text to be hidden unless javascript available.</p>

Pulsimeter answered 27/8, 2008 at 14:57 Comment(0)
O
0

There isn't a tag for that. You would need to use javascript to show the text.

Some people already suggested using JS to dynamically set CSS visible. You could also dynamically generate the text with document.getElementById(id).innerHTML = "My Content" or dynamically creating the nodes, but the CSS hack is probably the most straightforward to read.

Obannon answered 29/8, 2008 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.