views:

1343

answers:

4

When I try to close my Google docs tab with unsaved changes, this is what I get in my browser (FF 3.5).

Are you sure you want to navigate away from this page?

You have unsaved changes in this document. Click Cancel now, then 'Save' to save them. Click OK now to discard them.

Press OK to continue, or Cancel to stay on the current page.

My question is whether such alerts are part of the web app (gdocs for eg.) or are they given out by the browser? If latter, how is this done?

A: 

The alerts are part of the web application. View the source code and look at the javascript.

mcandre
Look for "onBeforeUnload".
John Fisher
I cannot see it in Google Docs source, at least.
Vijay Dev
It's likely buried in some obscure JavaScript file. Google uses scripts that make their JavaScript files essentially unreadable to a human.
ceejayoz
A: 

You are probably looking for "onUnload"

<body onUnload=alert('Please dont leave! I love you!');>

or

<body onUnload=exitfunction();>
lekimeki
This is not the correct usage of onunload for page exit control. Not only did you omit the necessary double quotes - all your doing here is triggering an alert, and not actually triggering the unload confirm dialog.
Peter Bailey
Hey, don't be mean. >:P
mcandre
I'm not being mean, I'm being to the point. The point here is to disseminate accurate, usable information. It's why we have "downvote" and "comment" mechanisms *in addition to* the ability to "answer" and "upvote" questions.
Peter Bailey
Awwwwwwwwwwwwwww :( But ok, i will keep it real
lekimeki
+14  A: 

By the browser. It's the onbeforeunload handler that returns the customized text of the dialog, which is only the middle of the three paragraphs - the other two paragraphs as well as the text of the buttons cannot be customized or otherwise changed.

window.onbeforeunload = function(){ return 'Testing...' }

Will yield a dialog that says

Are you sure you want to navigate away from this page?

Testing...

Press OK to continue, or Cancel to stay on the current page.

You can nullify this by setting the handler to null

window.onbeforeunload = null;

Which StackOverflow itself does in a few places ;)

Peter Bailey
Thanks Peter. I have looked into how the modern browsers behave wrt the onbeforeunload event and posted my findings. Please add on any other intricate details that you know.
Vijay Dev
+2  A: 
Vijay Dev
Google Docs must have mistaken Safari for Google Chrome, which displays much more informative buttons.
Eli Grey