How to detect with JavaScript wheter the user is in incognito mode or not.

How to detect if you are in Incognito mode with JavaScript in Google Chrome

In some cases and depending on the kind of websites that you work on ... if you know what i mean ... you will need to know wheter your user is navigating on the Google Chrome browser in the Incognito Mode or not. Chrome just like others browser saves cookies, sessions, history, form data, password, and temp data for future use, the incognito mode just save these information in a separated place and deletes it as the incognito window is closed. That means your browsing info can not be traced on local system. Outside your system everything is same for ISP Google and website that you are browsing.

If you are willing to display a warning for some reason to your user if this mode is being used, we'll share with you a little snippet that will help you to achieve this with JavaScript.

1. Check availability function

The isIncognito function will do the trick for you. This function basically verifies wheter the FileSystem API is available in the browser or not, as you may don't know, during the incognito mode, JavaScript is unable to access the FileSystem API due to the mentioned reasons.

Assuming that the user is working on Google Chrome or not (if the user isn't event using Chrome, it will return false always), the function will return a boolean that confirms if you are in this mode or not:

/**
 * Determine wheter the incognito mode of Google Chrome is available or not.
 * 
 * @param callback Anonymous function executed when the availability of the incognito mode has been checked.
 */
function isIncognito(callback){
    var fs = window.RequestFileSystem || window.webkitRequestFileSystem;

    if (!fs) {
        callback(false);
    } else {
        fs(window.TEMPORARY,
            100,
            callback.bind(undefined, false),
            callback.bind(undefined, true)
        );
    }
}

2. How to use it

To use the previous function, just call it and provide as first parameter a function that receives the flag variable as first parameter. According to the boolean value of the itIs variable, you can determine if you are in the incognito mode or not:

isIncognito(function(itIs){
    if(itIs){
        console.log("You are in incognito mode");
    }else{
        console.log("You are NOT in incognito mode");
    }
});

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors