Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I started learning express and I want to to know is there is a way to check if JavaScript is enabled/disabled in the browser from server side in express or the only way is to use the no script tag

myCode:

JavaScript
 //cookie Router
  const express = require('express')
  const {setCookie,deleteCookie} = require('../controllers/cookiesController')
  const cookiesRouter = express.Router()
  cookiesRouter.get('/cookies/set',setCookie)
  cookiesRouter.get('/cookies/delete:key',deleteCookie)


module.exports = cookiesRouter


JavaScript
  //cookie Controller
  exports.setCookie = async(req,res,next)=>{
    const browserOption = await req.query
    console.log(browserOption.javaEnabled)
    if(browserOption.javaEnabled){
        res.cookie('javaenabled',browserOption.javaEnabled,{path:'/'});
        res.send('cookie Enabled')
    }
}


JavaScript
//ajax call from browser sending the option that i want to add to cookie
let baseURl = 'http://localhost:4000'
console.log(window.navigator.javaEnabled())
function setCookies(){
    let url = `${baseURl}/cookies/set`;
    $.ajax({
        url:url,
        method:"GET",
        data: {javaEnabled:navigator.userAgent},
        success:function(data){
            alert(data)
        }
    })
}
setCookies()


What I have tried:

i have tried to use the
HTML
<noscript>
and this is the only working way to check if the javascript is enabled or not
Posted
Updated 20-Mar-22 4:45am
v2
Comments
Richard Deeming 21-Mar-22 6:27am    
The browser doesn't send any information to the server to indicate whether or not it will execute Javascript.

But the more important question is why you need to know? It's always better to use "progressive enhancement": write your site in such a way that the basic functionality is usable without Javascript, and only use script to enhance the user experience.
Member 14479161 23-Mar-22 0:39am    
I am using express in server side all of the functionality of express stoped if the JavaScript is disabled that's why I am asking
Richard Deeming 29-Mar-22 3:44am    
Server-side Javascript will not stop working if client-side Javascript is disabled. Only the client-side functionality will be affected.

As I said, you can't detect on the server whether the client has disabled Javascript - at least not with a single request. (Technically, you could serve up the JS-disabled page, and then use Javascript on that page to load the JS-enabled page.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900