Difference in Browser and tab close event for beforeunload

606 Views Asked by At

I'm working with Reactjs and have this requirement where I need to differentiate between Browser close event and Tab close event. I'm using 'beforeunload' for this, but the event is called for both the events (browser and tab close).

My code looks like this -

componentDidMount() {
    window.addEventListener('beforeunload', this.handleUnload);
}

componentWillUnmount() {
    window.removeEventListener('beforeunload', this.handleUnload);
}
 
 handleUnload(e) {
    var message = "\o/";
    console.log(window.event);
    (e || window.event).returnValue = message; //Gecko + IE
    return message;
 }

Can Someone tell me how can I differentiate for these two events.

0

There are 0 best solutions below