-1
<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.

  • Please tell me if any detail is missing in the question so I can update it. – Sleepinggod Jun 28 '21 at 05:36
  • 1
    Is this a React question? What time do you want to save? Where do you want to save it? – Drew Reese Jun 28 '21 at 05:40
  • Need to save the time when we click on leave page. I will save it in backend once I can get the value in the console.log, I just need to get it in console.log. – Sleepinggod Jun 28 '21 at 05:44
  • Yes, We are working on reactjs – Sleepinggod Jun 28 '21 at 05:44
  • Can you include React code in your question then? Also, what time are you wanting to save? Does this help answer your question? https://stackoverflow.com/questions/64966559/is-there-a-function-like-componentwillunmount-that-will-fire-before-the-page-i/64967211#64967211 – Drew Reese Jun 28 '21 at 05:54

2 Answers2

2

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.

-1

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>