[Help] How can I show an exit popup in a specific case?

Hi, I want to show an exit popup by using onbeforeunload like below.

<script>
    window.onbeforeunload = function () {
        if (specific case) return "show popup"; // show popup and then close window.
        else return "";  // just close window
    }
</script>

But the specific case what I want is a global variable(bool) on a C# script (This script is a singleton instance).
How can I get this C# script variable state in the index.html?

I solved this problem after reading this thread.

I replaced the cs global variable into my index.html var.
And when I want to change a state of this variable, I called a .jslib method at a c# script.