-1

I want to use two javascript events: onbeforeunload and inside it I need to use onhashchange. The point is on leaving the page if user has filled some fields then the confirmation message appear warning user whether they want to stay on page or leave.

My code is following

       window.onbeforeunload = function (e) {
      window.onhashchange = function (r) { return ''; }
        };

But the above code does not seem to be working? what am I doing wrong?

[Updated] Again, what I am trying to achieve is I want to alert a user if the leave the page and if they have filled some fields on the page

user4545762
  • 69
  • 1
  • 9
  • 1
    You can not handle one event inside the handler for another event. What you are doing here, is bind the hashchange event only when beforeunload is fired, but even that makes little sense. Please describe what problem you are actually trying to solve here (and _not_ by just repeating what you have already said). – CBroe Feb 10 '15 at 09:16
  • I have updated what I exactly trying to achieve, please check – user4545762 Feb 10 '15 at 09:30
  • possible duplicate of [Trying to detect browser close event](http://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event) – Ravimallya Feb 10 '15 at 11:00

1 Answers1

0

You can try

window.onbeforeunload = function (e) {
       if (content on page changed) { confirm("Do you want to leave page?") } 

        };
window.onhashchange = function (r) { if (content on page changed) { confirm("Do you want to leave page?") } }

I have just given pseudo code, you can replace with actual code. Basic mistake is you are trying to bind event onhashchange in onbeforeunload.

Maha
  • 66
  • 5