[英]Developing page where if we close the tab then the closing time should be saved
<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.我在 index.html(reactjs) 文件中添加了它,所以当点击关闭窗口或选项卡时,它显示“你想离开”的警告标志,我需要在关闭选项卡时节省时间。
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.我使用了 onbeforeunload 和 onunload 事件侦听器,但由于我是初学者,因此无法完全理解。
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/有关在选项卡上处理浏览器活动的更多信息,您可以查看此链接https://www.geeksforgeeks.org/how-to-detect-browser-or-tab-closed-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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.