-1

I don't know if it's a good place for posting this kind of topic but after some research on how to detect if a browser doesn't support the window.close() method in JavaScript:

How do I programmatically detect how a browser handles window.close()?

Detecting if window.close worked

After many attempts I think I found a reliable solution for allowing this method to work.

I tested it on Mobile Safari (iOS) and Internet Explorer when the user answers No to the exiting confirmation. I posted this topic because I want to know how many browsers support the hack I've found and I need testers for it.

The code in question:

function selfclose(){
    var quit = true;
    window.close(); // try to quit
    quit = false; // will be executed if not out of app or not supported
    setTimeout(function(){ // give a little delay for allowing browser to exit without firing error message
        if(!quit){
            alert("You haven't quit the website or your browser doesn't support self-closing.");
        }
    },400);
}
Community
  • 1
  • 1

1 Answers1

0

How about feature detection? That would tell you at least whether the browser has the function. As to the behaviour of the browser I'm not sure it's possible to truly determine what the behaviour is going to be if it differs from the standard. It is a well established living standard.

var supportsclose = ("close" in window) 
&& typeof window["close"] === "function";

console.log(supportsclose);

https://developer.mozilla.org/en-US/docs/Web/API/Window/close

James South
  • 10,147
  • 4
  • 59
  • 115