0

I have seen some sites that apparently execute some type of browser function that prompts the user if he/she wants to leave the page because there is a form not submitted:

"This page is asking you to confirm that you want to leave - data you have entered may not be saved."

I know this can be programmed in JS, but is there a native function or option that suggests the browser to detect this kind of scenarios?

I believe there is one because I have seen several websites (like facebook) reacting in the same way and they all seem to use this kind of browser option.

like autocomplete="on / off" allows the browser to fill up a form or not automatically

Thanks

multimediaxp
  • 9,348
  • 13
  • 49
  • 80
  • I'm pretty sure there isn't something feature like autocomplete. But the website(s) you're referring to probably handle the same scheme - if a form or specific inputs are "dirty", and the user tries to leave the page, confirm it. But that's exclusively developed by the websites to correctly detect when to confirm – Ian Feb 16 '14 at 02:30

1 Answers1

2
<script language="JavaScript" type="text/javascript">
   //<![CDATA[
       window.onbeforeunload = function(){
           // Check the state of the form
            if(form_changed){
              return false;
            }else{
              return true;
            }

       };
   //]]>
</script>

When the function returns false to window.onbeforeunload, the browser automatically prompts the following question: "This page is asking you to confirm that you want to leave - data you have entered may not be saved." (firefox)

multimediaxp
  • 9,348
  • 13
  • 49
  • 80
Newse
  • 2,330
  • 1
  • 12
  • 7