简体   繁体   中英

Check if Javascript is enabled server side ASP.NET

Is it possible to check if the client browser has javascript enabled from ASP.NET code?

I was hoping to ideally do this on PreRender of controls, or PageLoad, so that I can change how they look.

Any suggestions, work arounds etc would be much appreciated.

You can't do it, without making a subsequent request I'm afraid.

There are explanations that can be found by doing a search on Google most use an AJAX call or a hidden field that gets popluated via some javascript, that won't get run if Javascript is disabled.

If you have to do it then I'd try doing it on the very first request from the client, and save a cookie (or somethign similar), then check the value of the cookie on subsequent requests. This assumes that the user doesn't enable / disable Javascript often. You could also store a value in the session.

I hope that helps

Page.Request.Browser.EcmaScriptVersion will indicate what ASP.NET thinks is true. This assumes that the BrowserCaps are correct. It does give you a first pass indication which is probably pretty close.

EDIT: I initially misunderstood the question (enabled vs. supported). You could use the BrowserCaps server side to weed out those UserAgents that don't support JavaScript. Then use one line of script on each request to determine if it is enabled via cookie:

// let the server know JavaScript is enabled via session cookie
document.cookie = "js=1; path=/";

Then detect existence server-side:

HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("js");
bool js = (cookie != null) && (cookie.Value == "1");

Once they close the browser this cookie will go away.

我认为这个artical将有用于从服务器端检查 javascript启用或不检查是否从服务器端启用了javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM