<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.
You can check for the browser event and track them using window event listeners. Something like :
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = "Please click 'Stay on this Page' and we will give you candy";
alert("Please click 'Stay on this Page' and we will give you candy")
//you can display you message here aslo can log time with new Date() to fetch system date and time.
});
For more information about handling your browser activity on tabs you can checkout this link https://www.geeksforgeeks.org/how-to-detect-browser-or-tab-closing-in-javascript/
I hope this will solve your issue.
Thanks to other guy, I've added the code and it's working well.
<script>
window.onbeforeunload = function (e) {
e.preventDefault();
e.returnValue = "Please click 'Stay on this Page' and we will give you candy";
var d = new Date().toLocaleTimeString();
console.log(d)
};
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.