Reputation: 1921
I am maintaining an XPages project developed in-house and have a weird problem. If the user starts at the “Home” page and drills down through categories to a specific record, i.e. record ID “9H4G7J”, which opens in a separate browser tab, then commences a change but then decides to exit without saving, by clicking on the “Exit” button a prompt states – “Are you sure you want to navigate away from this page? This document may contain unsaved changes” – and when the user clicks Okay the record tab closes and user presented with the previous browser tab with the location of the record drilled down to in the categories. This is good this is what we want.
BUT, if the user, instead of drilling down, enters the record ID “9H4G7J” in the search facility on the “Home” screen, then commences a change and clicks Exit, The same prompt outlined above is shown but when the user clicks okay a second prompt is shown stating –“The webpage you are viewing is trying to close this window. Do you want to close this window?” – and if the user clicks “Yes”, the browser closes entirely!!! Can anyone explain why this would behave differently like this?
Here is the Java code;
private UIComponent createEventHandler(FacesContext context,
UIComponent parent, PageExpressionEvaluator evaluator) {
XspEventHandler result = new XspEventHandler();
result.setSubmit(false);
result.setEvent("onclick");
MethodBinding script = evaluator.createMethodBinding(result,
"if (XSP._isDirty()){\n if (confirm (\"Are you sure you want to navigate
away from this page?\" + \"\\n\" + \"\\n\" +\n \"This document may contain
unsaved changes.\" + \"\\n\" + \"\\n\" +\n \"Press OK to continue, or
Cancel to stay on the current page.\")){\n window.close();\n }else
{\n return false;\n }\n} else {\n window.close();\n}",
null,null, null);
result.setScript(script);
return result;
}
And here is the XPage “Edit” button source code;
<xp:button value="Exit" id="button2">
<xp:eventHandler event="onclick" submit="false">
<xp:this.script><![CDATA[if (XSP._isDirty()){
if (confirm ("Are you sure you want to navigate away from this page?" + "\n"
+ "\n" + "This document may contain unsaved changes." + "\n" + "\n" +
"Press OK to continue, or Cancel to stay on the current page.")){
window.close();
}else{
return false;
}
} else {
window.close();
}]]></xp:this.script>
</xp:eventHandler></xp:button>
Thanks in advance
Upvotes: 0
Views: 585
Reputation: 20384
When an application opens a new tab or new window the JavaScript call window.close();
closes that tab or window. If you issue a close on the last window open, the browser closes. To see the effect: Open an additional tab with a new page open (dilbert.com is a good choice) and see the effect.
To make your application behavior consistent change the code to either always open (target=_blank) or never open a new tab/window
I personally hate if applications open new tabs for me. If I want a new tab I can hold the Ctrl key before clicking. But that's a question of taste and your users might like the auto new tab. Just make it consistent
Upvotes: 1