views:

1571

answers:

9

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 cant use the form if they don't have it.

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

Thanks

+20  A: 

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

ceejayoz
+1  A: 

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.

DannySmurf
A: 

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();

Ross
A: 

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>

A: 

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.

cpm
A: 

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.)

Shadow2531
+7  A: 

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.

Will
Interesting idea. <noscript> works in <head> blocks?
ceejayoz
Not allowed in the xhtml standard at least.
Dykam
+3  A: 

fist of all, always seperate content, markup and behaviour!

now, if youŕe using the jquery libary (you really should, it makes javascript a lot easier) the following code should do:


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

this'll give you an additional class on the body when JS is enabled. now, in css, you can hide the area with css when the js class is not availble, and show the area when js is availble.

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 stil displayed if CSS is disabled.

alexanderpas
+1  A: 

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"&gt;&lt;/script&gt;
    <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>
Chrisbloom7