Bug Tracker

Ticket #4106 (closed bug: fixed)

Opened 6 years ago

Last modified 3 years ago

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:
Blocking: Blocked by:

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>

Change History

comment:1 follow-up: ↓ 2 Changed 6 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to duplicate

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

comment:2 in reply to: ↑ 1 Changed 6 years ago by dr_teeth

Replying to 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".

comment:3 follow-up: ↓ 5 Changed 6 years ago by serious

  • Status changed from closed to reopened
  • Resolution duplicate deleted

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);

comment:4 Changed 6 years ago by brandon

  • Milestone changed from 1.3.2 to 1.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.

comment:5 in reply to: ↑ 3 Changed 6 years ago by PaleBlueDot

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 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);

comment:7 Changed 6 years ago by serious

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

comment:8 Changed 5 years ago by serious

Is there any chance of this getting sorted for 1.4?

comment:9 Changed 5 years ago by john

  • Status changed from reopened to closed
  • Version changed from 1.3.1 to 1.4a1
  • Resolution set to fixed

comment:10 Changed 5 years ago by serious

Thanks John that's great news :)

Note: See TracTickets for help on using tickets.