<script>
window.onbeforeunload = function (e) {
return "Please click 'Stay on this Page' and we will give you candy";
};
</script>
I added it in index.html(reactjs) file so when click on closing the window or tab, it shows the warning sign of 'Do you want to leave', I need to save the time when we close the tab.
I am unable to find way to save the closing time of browser/tab. Please tell me the possible solutions.
import React, { Component } from 'react';
//deleted imports to decrease code length
class PrivilegeReport extends Component {
constructor(props) {
super();
this.state = {
loaded: true
};
}
handleWindowClose(){
alert("Alerted Browser Close");
}
componentDidMount() {
document.title = "LIMS - Privilege Report ";
window.addEventListener("beforeunload", (ev) =>
{
ev.preventDefault();
return ev.returnValue = 'Are you sure you want to close?';
});
console.log("beforeunload")
}
componentWillUnmount() {
window.removeEventListener('onbeforeunload', this.handleWindowClose);
console.log("onbeforeunload")
}
render() {
!this.state.loaded ? startLoading() : stopLoading();
return (
this.state.loaded && <div>
<Header />
<Sidebar context="Priviledge Report" />
<main>
<div className="container">
<Breadcrumb {...{ context: 'REPORTS', leaf: 'Privilege Report' }} />
<div className="columns is-mobile">
<div className="column"><h1 className="title">Privilege Report</h1></div>
<div className="buttons">
<button className="button is-primary" onClick={e => { console.log("TBD...."); }}>Print</button>
<button className="button is-primary" onClick={e => { console.log("TBD...."); }}>Download</button>
</div>
</div>
<div className="columns">
<div className="column">
<Tabs>
<TabList>
<Tab><div className="tab-title">User to User Type Mappping</div></Tab>
<Tab><div className="tab-title">User Type to Policy Mapping</div></Tab>
</TabList>
<TabPanel><Grid status="UTU" /></TabPanel>
<TabPanel><Grid status="UTP" /></TabPanel>
</Tabs>
</div>
</div>
</div>
</main>
</div>
)
}
}
const mapStateToProps = state => ({
me: state.me
});
const mapDispatchToProps = dispatch => {
return {
//updateMe: data => dispatch(updateMe(data))
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(withRouter(PrivilegeReport));
I used event Listeners onbeforeunload and onunload but as I am a beginner so unable to understand things completely.