1

I am developing a website where I have many videos and want to store all video times in to DB before leaving the website just like NETFLIX. I have written REST service in the below function and this function will store data into my DB. But, i am unable to store the details into DB as website is getting closed as soon as rest service called. Is there any other way where I can achieve this?

$(window).bind('beforeunload', function(event) {

}
  • `$(window).unload(function(){ /* do stuff */ });` – StackSlave Jul 16 '20 at 01:40
  • Thank you @StackSlave for the quick response. I will try and let you know. – StackOverflow Asker Jul 16 '20 at 01:41
  • Take a look at using [Navigator.sendBeacon()](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon) – charlietfl Jul 16 '20 at 02:02
  • Does this answer your question? [Javascript onload and onunload](https://stackoverflow.com/questions/9907867/javascript-onload-and-onunload) – Vijay Palaskar Jul 16 '20 at 02:13
  • @StackSlave Website is getting closed before calling the REST service when i use $(window).unload(function(){ /* do stuff */ }); After closing the website tab, unload function is getting called and console log is printing but after that there is REST service and it is not getting called. – StackOverflow Asker Jul 16 '20 at 02:19
  • you need to Write service call in beforeunload event.check my answer window.addEventListener('beforeunload', function(event) { console.log('I am the 1st one.'); }) – Vijay Palaskar Jul 16 '20 at 02:21

1 Answers1

0

If you want to call service then you need to use brforeunload event.unload event only useful for console.log stuff

Using vanilla JavaScript:

window.addEventListener('beforeunload', function(event) {
       console.log('I am the 1st one.');
      });
  
     window.addEventListener('unload', function(event) {
         console.log('I am the 3rd one.');
        });

Using jQuery

        $(window).unload(function(){
            console.log("Goodbye!");
         });

you might consider using the onbeforeunload event in some browsers or you going use to alert then you need use [onbeforeunload][1] event.

Vijay Palaskar
  • 526
  • 5
  • 15
  • I have used $(window).unload(function(){ console.log("Goodbye!"); }); and when i did debugging, after closing the site, it stopped at console.log() but after that there is REST service and site is getting closed before calling the REST service. – StackOverflow Asker Jul 16 '20 at 02:13
  • you need to Write service call in beforeunload event.check my answer window.addEventListener('beforeunload', function(event) { console.log('I am the 1st one.'); }); – Vijay Palaskar Jul 16 '20 at 02:19
  • I have written the service in window.addEventListener('beforeunload', function(event) { console.log('I am the 1st one.'); }); I started debugging before closing the site tab, it is stopping at console statement but its not calling the service, i have checked the DB and java controller, it is not called. – StackOverflow Asker Jul 16 '20 at 02:28