Javascript – window.onbeforeunload error


We currently have a JS that popups a window whenever the visitor wants to leave the site. It works fine, except when hitting the back button on the browser, it popups up the alert even if we are on the site.
we want to fix this.

Please note:
-we do not want to disable the back button
-we do not want to erase the historial

the JS function is onbeforeunload.
it is currently loading everytime the user wants to leave the site. When the user navigates the site, on every link that we DO NOT want to trigger the popup, we add this: onClick="changes=true;" to the href.

As I mentioned, problem is when user hits back button, FWD and sometimes refresh.

here is the website url link text

Best Solution

Remove the onbeforeunload-Event entirely. Don't bully your visitors! If they want to leave your website, you shouldn't try to keep them there, it's annoying.

The only valid reason to use the onbeforeunload-event is when the user has changed something on the site and tries to navigate away from the site without saving the change.