2

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.

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75

2 Answers2

6

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • ok I will use this but pointy I have to delete this question because some users are voting for close the question, well.. thanks for the help.. – Deep Kakkar Feb 16 '17 at 15:46
  • But the answer which I am looking for I still did not got that – Deep Kakkar Feb 16 '17 at 15:47
  • You don't have to delete the question if you don't want to @Deep. – Pointy Feb 16 '17 at 15:48
  • @Deep you can use the fact that a user went to the special URL as a way to mark that session as having JavaScript disabled, but really if you just drop that ` – Pointy Feb 16 '17 at 15:49
  • yes it is working I just checked. I was in doubt I read it on SO http://stackoverflow.com/questions/121203 – Deep Kakkar Feb 16 '17 at 15:52
  • It used to be considered questionable, but it's now explicitly in the HTML5 specification. – Pointy Feb 16 '17 at 15:52
  • yes I will not delete the question because I am looking if I get the answer using node js.. may be any other way we have. – Deep Kakkar Feb 16 '17 at 15:52
  • I have previously used this solution with no avail, therefore I don't personally believe this answer to be a valid solution, I have spent the past week trying to get this "meta" to work on a non-enabled JS Page, and all it did was cause excessive amount of strain on the process. – Johnty Feb 18 '22 at 09:49
  • @Johnty this **definitely** works; I have used it in many applications. What exactly is going wrong? – Pointy Feb 18 '22 at 15:00
  • @Pointy I've disabled JS multiple times, used different browsers even development web browsers including mobile browsers using emulators as well as standard safari all with JS Disabled and my site never redirects/runs the `meta` within the ` – Johnty Feb 18 '22 at 15:38
  • @Johnty you may need to include the code snippet in this answer in the `` of your pages for it to work; I'm not 100% sure because it's been a little while since I've had to worry about it. – Pointy Feb 18 '22 at 16:25
2
  • 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

Makubex
  • 1,054
  • 1
  • 9
  • 16