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

扶醉桌前 提交于 2020-01-24 20:14:28

问题


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.


回答1:


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.




回答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



来源:https://stackoverflow.com/questions/42278286/how-to-check-javascript-is-enabled-or-not-in-node-js-server-side-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!