How do I detect microsoft edge, firefox, and other browsers

Hello everyone. I am using this script for detecting OS and browsers that was created in a prior post:

var sUsrAg
var nIdx
getBrowserId = function() {
var browsers = [“MSIE”, “Firefox”, “Safari”, “Chrome”, “Opera”];
sUsrAg = window.navigator.userAgent,
nIdx = browsers.length - 1;
for (nIdx; nIdx > -1 && sUsrAg.indexOf(browsers [nIdx]) === -1; nIdx–);

return browsers[nIdx];
psychoJS.experiment.addData(“OS”, window.navigator.platform)
psychoJS.experiment.addData(“Browser”, getBrowserId())

This script works when run in Chrome, but when I use it in Microsoft edge or Firefox, I get get errors during the initialization of the experiment. Firefox says: Uncaught ReferenceError: assignment to undeclared variable getBrowserId and Edge says the ‘viewport’ meta element ‘content’ attribute value should not contain ‘user-scalable’.

I am hoping someone with a better knowledge of psychojs could help me figure out how to adjust this code to detect a wider variety of browsers.

A simple but inelegant solution would be to just have this:

psychoJS.experiment.addData(“OS”, window.navigator.platform)
psychoJS.experiment.addData(“Browser”, window.navigator.userAgent)

It isn’t ideal in that your data file will get the full userAgent header, which includes more information than you strictly need, but I personally find it easier to process stuff like that in Excel then try to bully JavaScript into parsing it for me.

1 Like

Are you using jsPsych or PsychoJS (via the PsychoPy Builder interface)? If it’s the latter I’ll change the category of your thread.

PsychoJS. Sorry about that.

Thanks so much for the responses. Just letting everyone know that I essentially solved my problem by integrating Qualtrics into my experiment since it is able to detect the browser and OS.