How to detect if JavaScript is disabled?

后端 未结 30 3390
暗喜
暗喜 2020-11-21 07:37

There was a post this morning asking about how many people disable JavaScript. Then I began to wonder what techniques might be used to determine if the user has it disabled.

30条回答
  •  孤街浪徒
    2020-11-21 08:03

    I would like to add my solution to get reliable statistics on how many real users visit my site with javascript disabled over the total users. The check is done one time only per session with these benefits:

    • Users visiting 100 pages or just 1 are counted 1 each. This allows to focus on single users, not pages.
    • Does not break page flow, structure or semantic in anyway
    • Could logs user agent. This allow to exclude bots from statistics, such as google bot and bing bot which usually have JS disabled! Could also log IP, time etc...
    • Just one check per session (minimal overload)

    My code uses PHP, mysql and jquery with ajax but could be adapted to other languanges:

    Create a table in your DB like this one:

    CREATE TABLE IF NOT EXISTS `log_JS` (
      `logJS_id` int(11) NOT NULL AUTO_INCREMENT,
      `data_ins` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `session_id` varchar(50) NOT NULL,
      `JS_ON` tinyint(1) NOT NULL DEFAULT '0',
      `agent` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`logJS_id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
    

    Add this to every page after using session_start() or equivalent (jquery required):

    
            
            
    

    Create the page JSOK.php like this:

提交回复
热议问题