0

detect_reload() function will produce a message if webpage is reloaded by user.
What should be code for detect_reload() function?

function detect_reload()
    {

    }
Tom
  • 83
  • 9

2 Answers2

3

Check the following link. https://stackoverflow.com/a/10400239/3184797 It has stated that

If it is refreshing, window.onunload will fire.

// From MDN
window.onunload = unloadPage;
function unloadPage()
{
    alert("unload event detected!");
}

https://developer.mozilla.org/en/DOM/window.onunload

If you just want a confirmation box to allow them to stay, use this:

window.onbeforeunload = function() {
    return "Are you sure you want to navigate away?";
}
Community
  • 1
  • 1
S0haib Nasir
  • 232
  • 2
  • 12
  • That's what i needed.. Thanks @S0haib Nasir – Tom May 11 '16 at 08:24
  • Don't just copy/paste other answer. Mark question as duplicate instead. BTW, this doesn't specifically answer question. I'm really not sure OP has fully tested it... – A. Wolff May 11 '16 at 08:29
  • @A.Wolff if you notice i mentioned it as duplicate first in the comments. Then I gave him an answer what he was looking for with full credit to the post of the user who mentioned it. Will it be okay if i just wrote the code and ignore the link from where it was taken from? I thought we are here for help each other not humiliating others. He posted duplicate and he did get minus reputation for that. It works or not, I believe who marked it as answer has tested that as well. Adios! – S0haib Nasir May 11 '16 at 09:30
  • @S0haibNasir Oh no, i didn't notice it, my bad! But i don't see anyone humilating anyone else here. Just saying it won't work as OP would expect it to work. Or OP's question was badly asked. Now if futur readers come here with same expected behaviour: 'How to know that user has reloaded the webpage', i think it is more fair to let them know this won't answer it. I hope it makes sense – A. Wolff May 11 '16 at 09:36
0

When the page HAS been reloaded, not before:

Maybe you could use document.referrer for this?

$(function() {
   if(document.referrer === "") {
      //Your code here
   }
});
Arg0n
  • 8,283
  • 2
  • 21
  • 38
  • please can you explain what document.referrer will perform?? – Tom May 11 '16 at 08:13
  • I misunderstood your question. This is for when the page HAS been reloaded, not before. `document.referrer` will tell you from which page the user got to this page. But if it's the same page it will be an empty string. – Arg0n May 11 '16 at 08:14