我在我的javascript中使用window.onbeforeunload。 这在IE中完美运行,但不会在Firefox中触发。
我在stackoverflow中检查了不同的链接....在其中我读了那个window.onbeforeunload不受firefox支持。这是真的吗?
如果是,请告诉我在浏览器关闭时调用app.deleteAccount(密钥)的另一种方式。这是我的javascript。请查看deleteFile()和dontDeleteFile()方法。
<script type="text/javascript">
//Event handler for body onload
function confirmDeleteFile(){
var secured =document.r_form.Secure[1].checked;
alert("confirmDeleteFile : "+secured);
if(secured){
var agree=confirm("Are you sure you wish to continue?");
if (agree){
//document.form1.submit();
return true;
}
else
return false ;
}
// submitForm();
return true;
}
function deleteFile() {
alert('inside deleteFile() but outside window.onbeforeunload');
window.onbeforeunload = function(){
var key = DOMAIN+'::' + elem('userName').value;
alert('inside deleteFile()');
app.deleteAccount(key)
alert('Unloading!');
}
}
function dontDeleteFile() {
alert('inside dontDeleteFile() but outside window.onbeforeunload');
window.onbeforeunload = function(){
alert("Not deleting");
}
}
function validateFormOnSubmit(theForm) {
var reason = "";
var userName = theForm.username.value;
var pin = theForm.pin1.value;
var PAM = theForm.pam.value;
var organization = theForm.organization.value;
//reason += validateUsername(theForm.username);
reason += validateEmpty(theForm.pam);
reason += validatePinMatch(theForm.pin1,theForm.pin2);
reason += validatePin(theForm.pin1,theForm.pin2);
if (reason != "") {
if(!confirmDeleteFile()){
return false;
}
alert("Some fields need correction:\n" + reason);
return false;
}
else{
if(!confirmDeleteFile()){
return false;
}
<% String url = request.getServerName().toString();
int port = request.getServerPort();
String contextPath = request.getContextPath();
%>
var servlet = "arcotebank.az"; //AroctEBanking Servlet
var url = BASE_URL + '/' + servlet;
var query = 'lang=en&reqid=1&version=1.1';
query += '&device=' + urlEncode(navigator.userAgent);
query += '&uid=' + urlEncode(userName);
query += '&code=' + urlEncode(PAM);
query += '&pin=' + urlEncode(pin);
query += '&usePin=' + usePin+'&method=arcotOTPEnroll&organization='+organization;
//alert("url=>"+url + '?' + query);
var xml = app.openUrl(url + '?' + query) + '';
//alert("xml=>"+xml);
if(appError()){
alert("applet error");
}
var domain = getDomain(url);
app.provisionAccount(domain, xml);
if(appError()){
alert("applet error");
}
var acc = app.getAccount(DOMAIN + '::' + userName);
if(acc!=null){
<%String formSubmitAction1 =
URLEncoderDecoder.encodeURL(
formAction,
"Action.2FA.Arcot.Navigation.LogoutActionCalled=Y",cm);%>
theForm.action ='<%=formSubmitAction1%>';
var secured =document.r_form.Secure[1].checked;
alert("line 131 "+secured);
if(secured){
deleteFile();
}else{
dontDeleteFile();
}
theForm.submit();
}else{
document.getElementById("error").innerHTML = "Failed to Create ArcotOTP";
}
}
}
function resetForm(){
var form = document.forms[0];
form.username.value = '';
form.pam.value = '';
form.pin1.value = '';
form.pin2.value = '';
}
function validateUsername(fld) {
var error = "";
var illegalChars = /\W/; // allow letters, numbers, and underscores
if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter a username.\n";
} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
fld.style.background = 'Yellow';
error = "The username should contain more than 4 characters.\n";
} else if (illegalChars.test(fld.value)) {
fld.style.background = 'Yellow';
error = "The username contains illegal characters.\n";
} else {
fld.style.background = 'White';
}
return error;
}
function validateEmpty(fld) {
var error = "";
if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "You didn't enter Personal Assurance Message \n"
} else {
fld.style.background = 'White';
}
return error;
}
function validatePin(pin1,pin2){
var error="";
if(pin1.value!=pin2.value){
pin1.style.background = 'Yellow';
pin2.style.background = 'Yellow';
error += "Pin numbers dont match\n";
//alert("Pin numbers dont match");
}
return error;
}
function validatePinMatch(pin1,pin2){
var error="";
if(pin1.value==""){
//elem('otp').style.background = 'Yellow';
pin1.style.background = 'Yellow';
error += "Pin number cannot be empty\n";
//alert("Pin number cannot be empty");
}
if(pin2.value==""){
//elem('otp').style.background = 'Yellow';
pin2.style.background = 'Yellow';
error += "Confirm Pin number cannot be empty\n";
//alert("Pin number cannot be empty");
}
return error;
}
</script>
答案 0 :(得分:3)
请注意,在Firefox 4及更高版本中,返回的字符串不会显示给用户。见Bug 588292。
https://developer.mozilla.org/en/DOM/window.onbeforeunload
也许这就是造成你问题的部分原因。此外,页面上的示例可能更适合跨浏览器兼容性?
window.onbeforeunload = function (e) {
var e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Any string';
}
// For Safari
return 'Any string';
};
答案 1 :(得分:1)