Skip to content

Unload: update outdated example #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

arthurvr
Copy link
Member

Would fix #388.

@mgol
Copy link
Member

mgol commented Jan 28, 2015

I'd also add a note that in Firefox (and maybe in some other places?) a message is ignored.

@arthurvr
Copy link
Member Author

I'd also add a note that in Firefox (and maybe in some other places?) a message is ignored.

I thought about it, but are jQuery API docs the right place to teach people JavaScript? This isn't a jQuery specific behaviour.

@mgol
Copy link
Member

mgol commented Jan 28, 2015

We're already making this event special by setting returnValue by ourselves; people using the event directly have to know that. Since we're taking some responsibility off users, I think we should inform them we're not fixing everything. :)

What do you think, @dmethvin?

@@ -28,7 +28,7 @@
<p>Any <code>unload</code> event handler should be bound to the <code>window</code> object:</p>
<pre><code>
$( window ).unload(function() {
alert( "Handler for .unload() called." );
return "Handler for .unload() called.";
});
</code></pre>
<p>After this code executes, the alert will be displayed whenever the browser leaves the current page.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/alert/message/ maybe?

s/will be/may be/ as well?

@dmethvin
Copy link
Member

I believe we've gotten bug reports about this in the past, so perhaps we can add something to the warning already there.

@arthurvr
Copy link
Member Author

Just for the reference, dropping in jquery/learn.jquery.com#608 too. Might be worth it to mention it there.

@arthurvr
Copy link
Member Author

I believe we've gotten bug reports about this in the past, so perhaps we can add something to the warning already there.

Added a sentence warning about it. I don't think we should go in-depth. @dmethvin What do you think?

});
</code></pre>
<p>After this code executes, the alert will be displayed whenever the browser leaves the current page.
It is not possible to cancel the <code>unload</code> event with <code>.preventDefault()</code>. This event is available so that scripts can perform cleanup when the user leaves the page.
<p>Most browsers will ignore calls to <code>alert()</code>, <code>confirm()</code> and <code>prompt()</code> inside the event handler. Depending on the browser, the message you return will be used in a confirmation dialog. It is not possible to cancel the <code>unload</code> event with <code>.preventDefault()</code>. This event is available so that scripts can perform cleanup when the user leaves the page.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tab? ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops! Fixed!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mzgol Does the rest LGTY?

@mgol
Copy link
Member

mgol commented Jan 31, 2015

What about the info that some browsers will ignore the message at all?

@dmethvin
Copy link
Member

I agree the comments here should be kept simple and not go too deep. It's always a bit frustrating when an API reference turns into a big "how to write JavaScript in the browser" article. Fortunately this page is pretty short at this point so we're not adding that much more to read.

This event is available so that scripts can perform cleanup when the user leaves the page.

This is a great point, maybe move this to the beginning in its own paragraph? That brings up the async operation trap, so perhaps add a sentence after it like, "Note that the browser may cancel or deny asynchronous operations attempted in this handler, such as AJAX or setTimeout."

Depending on the browser, the message you return will be used in a confirmation dialog.

Maybe something like this instead? "The string you return may be used in a browser confirmation dialog, but not all browsers support this." The warning already covers this ground again so I don't think we need much more.

@mgol
Copy link
Member

mgol commented Jan 31, 2015

@dmethvin That sounds good. :)

Michał Gołębiowski

@arthurvr
Copy link
Member Author

arthurvr commented Feb 2, 2015

@mzgol @dmethvin Updated PR.

@dmethvin
Copy link
Member

dmethvin commented Feb 2, 2015

LGTM!

@arthurvr arthurvr closed this in e82e9db Feb 2, 2015
@arthurvr arthurvr deleted the unloadExample branch February 2, 2015 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

The unload docs have incorrect examples.
3 participants