[Seaside] Page with entry fields in a form and anchors

James Foster Smalltalk at JGFoster.net
Mon Aug 4 03:55:16 UTC 2008


> James Foster wrote:
>> Say I have a page that has entry fields in a form (with a submit  
>> button) but also on the page is one or more anchors. The anchors  
>> are not really intended to abandon the page, but just to update the  
>> display in some way (like the column names in a WATableReport that  
>> sort the rows) or do a call/answer to collect some more data for  
>> the current page. The problem is that if the user clicks on a link  
>> the data is the form is not processed. I understand why it works  
>> this way, but from a usability point of view it is unexpected. Is  
>> there some way (JavaScript?) of setting some trigger to trap  
>> navigation away from the page check for any modified fields and  
>> take some action (like an auto-submit or a confirm dialog)?
>>
>> James Foster

In case anyone is interested, I came up with the following JavaScript  
(with Scriptaculous) code that seems to do what I want in a  
generalized fashion. Any comments are welcome...

window.onbeforeunload = confirmExit;

var needToConfirm = true;
var inputElements = $$(''form input'');
var initialValues = currentValues();

function currentValues() {
	var values = [];
	inputElements.each(
		function(each) {
			values.push(each.getValue());
		}
	);
	return values;
}

function confirmExit()
{
	if (needToConfirm) {
		var values = currentValues();
		for (var i = 0; i < values.length; i++) {
			if (initialValues[i] !== values[i])
				return "Unsaved changes have been made to form input elements on  
this page.";
		}
	}
}

$$(''form button.submit'').each(
	function(each) {
		each.onclick = noNeedToConfirm;
	}
);

function noNeedToConfirm() {
	needToConfirm = false;
}



More information about the seaside mailing list