0
votes

I want my website to open only in these selected browsers (Chrome, FireFox, Safari, Opera & Edge). Can't figure out how i can make it happen, because the userAgent gives these strings for each browsers mentioned above for desktops or laptops

Chrome: (Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36)

FireFox: (Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0)

Safari: (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36)

Opera: (Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 OPR/68.0.3618.125)

Edge: (Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 Edg/83.0.478.45)

Commonality between 4 of them is Chrome and Safari Keywords accept FireFox, but FireFox on android shows Safari keyword in the userAgent string, to see the userAgents strings for all these browsers please go to this link https://www.whatismybrowser.com/guides/the-latest-user-agent/ might get better idea.

Now navigator.userAgent.search / .match / indexOf any of these methods or any other method within the navigator gives no solid or unique Identity of the browser (does't matter either desktop, laptop or mobile devices).

All other not most commonly used browsers also shows Chrome or Safari keywords, either both or at-least one of them in desktops, tabs and mobiles, for example: Samsung devices userAgent string shows Samsung Browser but also Chrome or Safari keywords as well.

So please can anybody help me figure out what could be best script to identify the browser and let my website https://justfortrial.com load on only above mentioned browsers.

1

1 Answers

0
votes

Well i think i found a way to identify five browsers (which are mentioned above in the question) on any devices. Don't know it works on Apple products or not, because i don't have any. Please try on your apple products and let me know if it works or not. I am not a professional, just learn things online buy trial and error methods. So if it does not work on your windows or phones, please let me know.

`

var userAgentString = window.navigator.userAgent;
var vendor = window.navigator.vendor;
var mobile = /(Mobile)|(Android)/i.test(userAgentString);
var browserCheckList = userAgentString.split(")");  // Splitting the string and creating a list 
var Edge = /Edg(e|A|iOS)?/i;
var Opera = /OPR/i;
var FireFox = /(Firefox)|(FxiOS)/i;
var Safari = /Safari/i;
var Chrome = /(Chrome)|(CriOS)/i;
var BrowserList = [Edge, Opera, FireFox, Safari, Chrome];
var count = 0;

for (var i = 0; i < BrowserList.length; i++) {
    // Checks the regular expressin in the userAgent String
    if (BrowserList[i].test(userAgentString)) {
        if (vendor === "Apple Computer, Inc.") {
            if (BrowserList[i] === Safari) {
                break;
            }
            else {
                count += 1;
                continue;
            }
        }
        else if (vendor === "Google Inc.") {
            // Removes all unwanted characters like (Dot, Forward Slash, Numbers and empty spaces)
            browserCheckList[2] = browserCheckList[2].replace(/\s?[/]?\.?[0-9]?/g, "");
            // When browser opens in desktop/Laptop
            if (!mobile) {
                // Checking for Chrome Browser
                if (BrowserList[i] === Chrome && browserCheckList[2].length === 12) {
                    break;
                }
                // Checking for Edge Browser
                else if (BrowserList[i] === Edge) {
                    break;
                }
                // Checking for Opera Browser
                else if (BrowserList[i] === Opera) {
                    break;
                }
                else {
                    count += 1;
                    continue;
                }
            }
            // When browser opens in Mobiel Phones
            else {
                if (BrowserList[i] === Chrome && browserCheckList[2].length <= 18) {
                    break;
                }
                else if (BrowserList[i] === Edge) {
                    break;
                }
                else if (BrowserList[i] === Opera) {
                    break;
                }
                else {
                    count += 1;
                    continue;
                }
            }
        }
        else if (vendor === "") {
            if (BrowserList[i] === FireFox) {
                break;
            }
            else {
                count += 1;
                continue;
            }
        }
    }
    else {
        count += 1;
        continue;
    }
}
// If none of the five browser is found in the userAgent String
if (count === 5) {
    // Your code here or redirect to Supported Browser HTML Page
    location.href = "supported_browsers.html";
}

` For sure it works on Windows at-least for me.