Detect when JavaScript is disabled in ASP.NET
Asked Answered
I

5

11

In the Render method of an ASP.NET web-control, I need to alter the output of the Html based on whether JavaScript is enabled or disabled on the clients browser,

Does anyone know the right incantation to figure that out?

Isomorph answered 29/10, 2008 at 11:36 Comment(0)
K
13

The problem with using script to check whether javascript is enabled is that you only find that out after the script hasn't run.

Some solutions try the opposite - they use javascript to set a value and then supply Javascript enabled controls if that value is later detected. However, this fails with javascript-sometimes-enabled tools like the Firefox NoScript plugin.

A more robust solution is to always send the plain-HTML compatible control, and then have javascript run on page load to add the correct event handlers/extra DOM elements/etc.

However I don't know how this fits in with the ASP.NET approach takes to controls.

Kaz answered 29/10, 2008 at 11:47 Comment(1)
I think you're right, it's probably better to assume that JS is disabled and then run script to transform the elements when it hits the client. Thanks.Isomorph
D
3

The noscript tag is used to define an alternate content (text) if a script is NOT executed.

EDIT: Hi Kieron, take a look at this link: Creating a Server Control for JavaScript Testing, and Detect if JavaScript is enabled in ASPX

Dormitory answered 29/10, 2008 at 11:39 Comment(1)
I was looking for something a little more fine-grained than that. I actually need to render different elements if JS is disabled. e.g. <ul> <li> // JAVASCRIPT ENABLED <img ... /> // JAVASCRIPT DISABLED <input ... /> </li> </ul>Isomorph
M
3

The browser does not communicate Javascript availability with the request.

One thing that I have done, though I'm not sure it is applicable in your case, is generate the page as if Javascript is not enabled, then have some Javascript that turns off the HTML-only stuff and turns on the HTML-Javascript stuff.

You could also have the first page hit "phone home" via Javascript to record in the session whether Javascript is enabled or not.

Monachism answered 29/10, 2008 at 11:49 Comment(2)
Sadly, javascript can be enabled or disabled without clearing the cookies, so the "phone home" method is not fail-proof.Dormouse
@JanDvorak - agreed, that wasn't and wouldn't be my first choice. I would think that in general it would be pretty safe as people who turn off javascript probably leave it off rather than changing mid-stream and if they do change mid-stream, they are likely to expect weird behavior. Broken scripts on subsequent pages are a different issue.Monachism
D
0

This is a way to check when a form is being submitted.

https://web.archive.org/web/20210428071600/http://www.4guysfromrolla.com/webtech/082400-1.shtml

I dont think there is any Request property that will tell you when the page is first requested. There is a property that will tell you if the browser supports JS, but not if its enabled. See Here

Dimissory answered 29/10, 2008 at 11:48 Comment(0)
R
0

What you need to do is this. After much testing and late nights, I decided to use the most simple solution. Drag a label onto the top of the page and make sure it reads "run at server". Then, for the Text attribute, put Text="This website requires Javascript". That should be the best answer. :D

Reformism answered 27/12, 2017 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.