Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write HTML code only if JS is enabled? [duplicate]

Tags:

javascript

dom

Possible Duplicate:
Is there a html opposite to noscript

<noscript> makes it easy to have HTML code as a fallback if JS is disabled... but what if I want to have HTML code which is only shown when scripts are enabled? I can have a JS block which dynamically writes HTML, but is there a nicer way to do it using some regular HTML?

let's say I have a link:

<a href="test.com">This should only appear if Javascript is enabed</a>
like image 226
Mr. Boy Avatar asked Mar 03 '11 13:03

Mr. Boy


People also ask

How do I disable JavaScript If a website is disabled?

The easy and simple solution is using the HTML “noscript” tag to display different content if JavaScript is disabled. See following full “noscript” example : If JavaScript is disabled : Display warning message and hide the entire content with CSS.

What if JavaScript is disabled in browser?

If JavaScript has been disabled within your browser, the content or the functionality of the web page can be limited or unavailable.

Which tag will display message if JavaScript is not enabled?

The <noscript> tag defines an alternate content to be displayed to users that have disabled scripts in their browser or have a browser that doesn't support script.


1 Answers

Use a HTML element with style="display:none", set that to display:block from JavaScript.

Sample code (ugly as hell, but just to give you the idea)

<div id="hideThisFromNonJs" style="display:none">
Bla bla bla
</div>

<script type="text/javascript">
document.getElementById('hideThisFromNonJs').style.display='block';
</script>
like image 159
Sean Patrick Floyd Avatar answered Oct 03 '22 04:10

Sean Patrick Floyd