📄 onbeforeunload.jsp
字号:
<!-- onBeforeUnload.jsp - Start -->
<SCRIPT LANGUAGE="JScript" TYPE="text/javascript" FOR=window EVENT=onbeforeunload>
return doOnBeforeUnload();
</SCRIPT>
<SCRIPT LANGUAGE="JScript" TYPE="text/javascript">
// Create variable we can change to prevent the check from being done.
var checkForChanges = true;
function doOnBeforeUnload() {
// This script fires when anything is about to cause the current page to be unloaded.
// If any unsaved changes have been made, this script returns a non-empty string, which
// causes a response window to warn the user that changes will be lost, and to allow
// them to cancel.
// In free form, tabular, and select screen sections built by the UI builder,
// the preSubmit method fired by the onSubmit event of the form removes this script from
// the onBeforeUnload event, which prevents this check from happening if the Save button
// was just clicked.
// alert("event.fromElement: " + event.fromElement);
// See if the checkForChanges flag has been cleared. If so, just allow the
// body unload to continue.
if (!checkForChanges) return;
// Look at all forms on the page to see if any of them have any changes.
var vFormC = document.forms;
for (var vFormNbr = 0; vFormNbr < vFormC.length; vFormNbr++) {
//alert("[verifyClose] Window name: " + window.name);
//alert("[verifyClose] Checking form #" + vFormNbr);
var vFormObj = vFormC.item(vFormNbr);
//alert("[verifyClose] Form name is " + vFormObj.name);
// Check the action. If it's query or view mode, don't bother checking this form.
var vActionObj = vFormObj.elements.item("action");
if (vActionObj != null) {
var vAction = vActionObj.value;
if (vAction!=null) {
//alert("Action: " + vAction);
if (vAction=="<%=UIScreenSection.ACTION_INSERT%>" ||
vAction=="<%=UIScreenSection.ACTION_UPDATE%>" ||
vAction=="<%=UIScreenSection.ACTION_UPDATE_SELECT%>") {
// This is an updateable form in an updateable mode. Check for changes.
// Look through all the objects in the form to see if there are any unsaved changes.
var vElemC = vFormObj.elements;
for (var vElemNbr = 0; vElemNbr < vElemC.length; vElemNbr++) {
var vElemObj = vElemC.item(vElemNbr);
// Find out if this is the "add" or "delete" select object that appears on a "select" type screen section.
//alert("[window.onbeforeunload] Object name: " + vElemObj.name);
if (vElemObj.name.indexOf("DBSel")>=0) {
// This is the add or delete select object. See if there are any items in it. If so, it means
// the user has made changes.
if (vElemObj.length > 0) {
// Changes have been made. Trigger the response window.
return "IF YOU CLICK OK, YOUR CHANGES WILL BE LOST.";
} else {
// No changes in this add or delete select object.
}
} else {
// Object is not the add or delete select object.");
// See if this element is an original value field.
var vOrigPrefix = "<%=UIWebUtility.HTML_NAME_PREFIX_ORIGINAL%>";
if (vElemObj.name.indexOf(vOrigPrefix)==0) {
// This is an original value field. Compare its value to the current value field.
var vOrigElemObj = vElemObj;
var vOrigElemName = vOrigElemObj.name;
var vCurElemName = vOrigElemName.substring(vOrigPrefix.length, vOrigElemName.length);
var vCurElemObj = vElemC.item(vCurElemName);
var vCurElemTagName = vCurElemObj.tagName;
var vCurElemType = vCurElemObj.type;
if (vCurElemObj!=null) {
// Got the current field. Compare the values.
var vCurValue;
if (vCurElemTagName=="INPUT" && vCurElemType=="checkbox") {
// This is a check box. Use special processing to get current value.
vCurValue = vCurElemObj.checked ? "Y" : "N";
} else {
// Not a check box.
vCurValue = vCurElemObj.value;
}
var vOrigValue = vOrigElemObj.value;
if (vCurValue != vOrigValue) {
// This field was changed. Trigger the response window.
// alert('Field ' + vCurElemName + ' changed.');
// alert('Original Value: ' + vOrigValue);
// alert('Current Value: ' + vCurValue);
return "IF YOU CLICK OK, YOUR CHANGES WILL BE LOST.";
}
}
}
}
}
}
}
}
}
}
</SCRIPT>
<!-- onBeforeUnload.jsp - End -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -