Firefox 中的 window.onbeforeunload 支持

发布于 2024-09-19 21:45:32 字数 5038 浏览 5 评论 0原文

我在 JavaScript 中使用 window.onbeforeunload 。 这在 IE 中完美运行,但在 Firefox 中不会触发。

我检查了 stackoverflow 中的不同链接......在其中我读到 firefox 不支持 window.onbeforeunload 。这是真的吗?

如果是,您能否告诉我在关闭浏览器时调用 app.deleteAccount(key) 的不同方式。这是我的 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>

I am using window.onbeforeunload in my javascript.
This works perfectly in IE but is not triggered in Firefox.

I checked on different links in stackoverflow.... In it I read that window.onbeforeunload is not supported by firefox. Is this true?

If yes, can you please tell me a different way to call app.deleteAccount(key) on close of browser. Here is my javascript. Please look at the deleteFile() and dontDeleteFile() methods.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网名女生简单气质 2024-09-26 21:45:32

请注意,在 Firefox 4 及更高版本中,返回的字符串不会向用户显示。请参阅错误 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';
};

Note that in Firefox 4 and later the returned string is not displayed to the user. See Bug 588292.

https://developer.mozilla.org/en/DOM/window.onbeforeunload

Maybe this is what's causing your problem in part. Also the example on the page might be better suited for cross-browser compatibility?

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';
};
赤濁 2024-09-26 21:45:32

window.onbeforeunload 受 Firefox 和所有主流浏览器支持。它应该被实现为一个返回字符串的函数,该字符串将用作确认对话框中的消息。

window.onbeforeunload is supported by Firefox and all major browsers. It should be implemented as a function that returns a string, which will be used as the message in the confirmation dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文