Deep Kakkar
Deep Kakkar

Reputation: 6305

How to check javascript is enabled or not in Node JS server side code

Is this possible to check this using server side code in Node js? OR if not then how can I use conditions like if-else:

if enabled then 
  do this
else
  do that

In a node.js project, I have to check that JavaScript is enabled or not on browser of user.

I know that we can check this using <noscript> tag at client side code(I am using jade). On Jade page I have inserted <noscript> tag which displays message that JavaScript is disabled. e.g.

noscript
 .noscriptmsg(style="color:red; padding:15px;")
   | You don't have javascript enabled on your Browser. Please enable to use complete functionality.
   | To know more
     a(href='http://enable-javascript.com/')  
      | Click Here

this is good to display the error message to user.

But I wants to know it on my server side code in node.js. I am using express framework.

I wants to make case for each and every page if JavaScript is disabled then user cannot move further OR in running app if user will disable JavaScript then app will be redirected on JS-No Found page.

OR some kind of conditional part in <noscript> part.

Can anyone please guide me how can I detect JS enable or disable on server side in Node.JS.

Upvotes: 2

Views: 3400

Answers (2)

Pointy
Pointy

Reputation: 413966

You can put a <meta> tag in a <noscript> block to redirect to some special URL:

<noscript>
  <meta http-equiv=refresh content='0; url=http://your.domain/noscript'>
</noscript>

You cannot of course be 100% certain that users who land on the special URL are there because they've disabled JavaScript, but for people not doing anything weird (other than disabling JavaScript) it works.

Note that checking "in server-side code" does not make sense; the only place you can check if a client has JavaScript enabled is in code that's sent to the client.

Upvotes: 7

Makubex
Makubex

Reputation: 1114

  • You can expose a GET / POST endpoint on your express app - ex: /jsenabled/

  • In the html page - On document ready you can call '/jsenabled'

  • In the jsenabled express handler -infer the user from the session / cookies

  • By default assume user has no js until you get this call

Upvotes: 2

Related Questions