Skip to main content

Bug Tracker

Side navigation

#4106 closed bug (fixed)

Opened February 10, 2009 03:31PM UTC

Closed December 07, 2009 12:56AM UTC

Last modified March 14, 2012 12:01PM UTC

binding to beforeunload does not work

Reported by: dr_teeth Owned by: brandon
Priority: major Milestone: 1.4
Component: event Version: 1.4a1
Keywords: beforeunload Cc:
Blocked by: Blocking:
Description

A simple html page that binds to the beforeunload event does not work. The function gets called, but the alert box does not appear.

Test case below:

<head>

<title>Untitled Page</title>

<script src="js/jquery-1.3.1.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(function()

{

$(window).bind("beforeunload",

function(e)

{

e.returnValue="Test";

return "Test";

} );

});

</script>

</head>

<body>

Test page

</body>

Attachments (0)
Change History (9)

Changed February 12, 2009 03:04AM UTC by dmethvin comment:1

resolution: → duplicate
status: newclosed

This was discussed in #1365 and closed as wontfix because the browsers don't treat this event normally. See the discussion there.

Changed February 17, 2009 07:02PM UTC by dr_teeth comment:2

Replying to [comment:1 dmethvin]:

This was discussed in #1365 and closed as wontfix because the browsers don't treat this event normally. See the discussion there.

Can this be reflected and explicitly mentioned in the documentation? It states in $.bind() that you can bind to "beforeunload".

Changed April 23, 2009 07:09AM UTC by serious comment:3

resolution: duplicate
status: closedreopened

I think further discussion or work is warranted.

I'd like to know why 'beforeunload' works perfectly in Adobe Spry but not jQuery?

There's no specific 'beforeunload' code in Spry so perhaps there are issues with the way jQuery attaches the events?

// ACTION FUNCTION
warnFormModified = function(e){
	returnValue = "This form contains unsaved changes.\\nYou will LOSE YOUR UPDATES if you press ok.\\n\\nTo save your work, press CANCEL below, then click\\nthe UPDATE BUTTON in the page.";
	if (!e) {
			e = window.event;
	}
	else
		if (e) {
			e.returnValue = returnValue;
		}
		else {
			return returnValue;
	}
};
//
// ADOBE SPRY WORKS
//
Spry.Utils.addEventListener(window, "beforeunload", warnFormModified, true);
//
// JQUERY DOES NOT WORK
//
jQuery(window).bind("beforeunload", warnFormModified);

Changed April 23, 2009 05:57PM UTC by brandon comment:4

milestone: 1.3.21.3.3

I'll take a look at this. I think we could utilize the special events api to work-around the cross-browser issues with this event.

Changed May 01, 2009 08:28AM UTC by PaleBlueDot comment:5

Prototype also handles it correctly.

I tested a simple example that I was porting from Prototype. It works in Prototype but not jQuery. Searching Prototype's source, there's no specific mention of 'beforeunload', so I assume that it's treated like any other event, too - FWIW.

Replying to [comment:3 serious]:

I think further discussion or work is warranted. I'd like to know why 'beforeunload' works perfectly in Adobe Spry but not jQuery? There's no specific 'beforeunload' code in Spry so perhaps there are issues with the way jQuery attaches the events?
> // ACTION FUNCTION
> warnFormModified = function(e){
> 	returnValue = "This form contains unsaved changes.\\nYou will LOSE YOUR UPDATES if you press ok.\\n\\nTo save your work, press CANCEL below, then click\\nthe UPDATE BUTTON in the page.";
> 	if (!e) {
> 			e = window.event;
> 	}
> 	else
> 		if (e) {
> 			e.returnValue = returnValue;
> 		}
> 		else {
> 			return returnValue;
> 	}
> };
> //
> // ADOBE SPRY WORKS
> //
> Spry.Utils.addEventListener(window, "beforeunload", warnFormModified, true);
> //
> // JQUERY DOES NOT WORK
> //
> jQuery(window).bind("beforeunload", warnFormModified);
> 

Changed June 05, 2009 12:21AM UTC by serious comment:6

Any news on getting this fixed? Also can someone remove the spam post by 'jessica' above?

Changed November 13, 2009 10:19AM UTC by serious comment:7

Is there any chance of this getting sorted for 1.4?

Changed December 07, 2009 12:56AM UTC by john comment:8

resolution: → fixed
status: reopenedclosed
version: 1.3.11.4a1

Changed December 07, 2009 01:15AM UTC by serious comment:9

Thanks John that's great news :)