5

Want to make a confirm box appear before someone leave my site. They have information that will go lost if they leave and you don´t want that right. Pleas be specific with were I should put the code and so since I´m not the best of coders...

Xweque
  • 605
  • 1
  • 9
  • 29
  • possible duplicate of [Warn user before leaving page with unsaved changes](http://stackoverflow.com/questions/7317273/warn-user-before-leaving-page-with-unsaved-changes) -- please use the search before you ask a new question. – Felix Kling May 12 '12 at 18:56

1 Answers1

7
window.onbeforeunload = function(){
    return "Are you sure you wanna leave my site?";
}
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
  • 1
    Note that although this will produce a dialog box in all browsers, in FF4 and up it won't show the user your text ([according to MDN](https://developer.mozilla.org/en/DOM/window.onbeforeunload)) - https://bugzilla.mozilla.org/show_bug.cgi?id=588292. – jimw May 12 '12 at 18:54
  • 4
    That code 'executes' whenever the unload event fires. Where you put the code varies depending on what code you're running on your site, how it's being loaded, etc. That kind of question is outside of the scope of StackOverflow IMO, as it's not really a specific programming question, but rather a question about how JS as a whole works. If you're asking this kind of question, it might be helpful to refer to this tutorial https://developer.mozilla.org/en/A_re-introduction_to_JavaScript and use it to generate more specific queries. – brandwaffle May 12 '12 at 19:07