0

I have tried below code and it is detecting whether current request browser is supports to java script or not only. But my actual requirement is detect particular request browser that whether java script is enabled or disabled by the time request hit the page_load method

var request = HttpContext.Current.Request;
bool jsEnabled = request.Browser.JavaScript;

if (!jsEnabled)
{
    javaScriptEnabled = false;
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Hezron Naresh
  • 79
  • 2
  • 12
  • Impossible to know in advance. Why do you need to do that? – Ry- Aug 15 '16 at 08:39
  • I have a method to call if js enabled otherwise call different method within the page_load block – Hezron Naresh Aug 15 '16 at 08:43
  • What does the method do? – Ry- Aug 15 '16 at 08:44
  • @Ryan I have a page which save the user browser details for stats and redirect to another page/link(third party link). So if js not enabled then without saving the user's browser details I have to redirect to another link/page (third party) – Hezron Naresh Aug 15 '16 at 08:47
  • @HezronNaresh why would the user having JS disabled mean that you can't save their browser details? – ADyson Aug 15 '16 at 08:49
  • Yes of course. I can not save their data as well as and I need to redirect to different page/link if js disabled or if not redirect to totally a different page/link. – Hezron Naresh Aug 15 '16 at 08:54
  • @HezronNaresh yes but I'm asking what the practical difference is. why can't you save the data is JS is disabled? Why not just do the same thing in both cases? The scenario doesn't make a lot of sense to me – ADyson Aug 15 '16 at 10:16
  • @ADyson I need to do 2 different thing in page_load based on js enabled and disabled kind of tracking the browser activities. – Hezron Naresh Aug 15 '16 at 10:54
  • @HezronNaresh but that doesn't stop you saving the browser information entirely during page load, which is what you seemed to be implying. The problem is that you can't know specifically whether JS is enabled or not. So you can't redirect to a different page based on that. Instead of the different redirect (to an error page I guess?) you'll have to use ` – ADyson Aug 15 '16 at 11:18

1 Answers1

0

That's not possible, you're using a server side language, it does not access that kind of property when the user executes a request.

The client may provide some meta info through HTTP headers, but they don't necessarily tell you whether the user has JavaScript enabled or not and you can't rely on them anyway.

Source : Check if JavaScript is enabled with PHP

The only way to know it's on the client side using a <noscript></noscript> tag.

Besides , request.Browser.JavaScript tells you if the browser supports javascript, not if it's enabled.

Community
  • 1
  • 1
Carlos Delgado
  • 2,930
  • 4
  • 23
  • 49
  • Thanks for response. But is it possible to check by using tag before page_load method? – Hezron Naresh Aug 15 '16 at 08:51
  • As you're unable to execute javascript, you can't take care when, or how to handle events. The only use of is to put some html to warn the user. . The – Carlos Delgado Aug 15 '16 at 08:53