0

I am trying to update the DB when a user closes/reloads the page. I have tried all sort of combinations with "onunload", "onbeforeunload", and "onhide" but nothing works.

Here's the code I would like to run when the page is closed:

function saveCurrentOutline () {    
db.get(document.getElementById("selectedTab").dataset.id).then(function(doc) {
        doc.resume = document.getElementById("outlineSelected").dataset.id      
        return db.put(doc);
    });     
}

Any suggestion as to how I can accomplish that?

*Edit: I am trying to run it on Chrome

Andre Hissa
  • 1
  • 1
  • 3
  • 1
    Posting the code you've tried to make this work on page close would be much more helpful than the code that you want to call when this happens. In this case we'll be able to point if there's something wrong with it. Also, please specify the browser you use: `onunload` and `onbeforeunload` are not supported in the same fashion by all the browsers. – YakovL Sep 18 '16 at 00:50
  • Thanks for answering, its my first question, so I am not quite sure how to express my question. You mean the html or the js or both? The js has about 900 lines, if there is there an part of it you find that would be helpful I would gladly post. If its about the function call, I can adapt the rest of my code to anything that would make this function work – Andre Hissa Sep 18 '16 at 00:56
  • I mean those bits where you set the event listener. HTML is probably irrelevant here. If you don't use jQuery, your probably use the `addEventListener` function. Or is it just some `
    `? In other words, try to add an http://stackoverflow.com/help/mcve that allows to reproduce and see your issue.
    – YakovL Sep 18 '16 at 01:36

1 Answers1

0

It worked with:

<body onunload ="saveCurrentOutline ()">

I am pretty sure I had tried the same thing yesterday but didn't work, I don't know where was the mistake. Thank you everyone for trying to help, and I am sorry that I lost your time with a simple blunder on my part.

Andre Hissa
  • 1
  • 1
  • 3