Hack4Life
Hack4Life

Reputation: 563

Hide placeholder in ASP when Javascript is disabled

I need to check if Javascript is enabled or not. If it is disabled I need to hide a placeholder. In this placeholder is a widget that is based on JS (jQuery). When Javascript is disabled the user can only see a white box with wasted space.

I found out that I can use the <noscript> Tag to display content (for example a <p> with an ID to identify this state) when JS is disabled but I don't know how to implement a function that checks if this ID is shown to the user or not.

When the ID is shown it should hide the placeholder. Is there any solution for doing this with C#?

Thanks for any help.

Upvotes: 1

Views: 468

Answers (2)

Philip Pittle
Philip Pittle

Reputation: 12295

You can set a CSS class on your body tag (or any appropriate parent tag) when you render the markup, and then use Javascript to change that class. When the class changes you can set different CSS stylings, including hiding markup:

Initial html:

<body class="no-js">

<div class="placeholder"></div>
</body>

Then some jQuery:

 $( document ).ready(function() {
     //Remove the no-js class and replace it with js
     $(".no-js").addClass('js');
     $(".no-js").removeClass('no-js');
 });

And some CSS:

 .no-js .placeholder{
    display: none;
 }

This way your CSS rule will default to hiding the placeholder, but jQuery will turn it back on again.

EDIT: jQuery is a little bit case sensitive ;)

Upvotes: 1

morkro
morkro

Reputation: 4655

You can do that with JavaScript as well. Sounds weird but yes.

You even don't have to check for an ID if it's shown or not. Just hide the placeholder initially with CSS. Then, if JavaScript is enabled, show the placeholder.

Upvotes: 1

Related Questions