问题描述
我正在使用window.onbeforeunload在我的JavaScript。这在IE中完美的工作,但没有在Firefox触发。
我检查了不同的链接在stackoverflow ....在这我读了window.onbeforeunload不支持Firefox。这是真的吗?
如果是,请告诉我一个不同的方法来在浏览器关闭时调用app.deleteAccount(key)。这是我的JavaScript。请查看deleteFile()和dontDeleteFile()方法。
< script type =text / javascript>
// body onload的事件处理函数
函数confirmDeleteFile(){
var secured = document.r_form.Secure [1] .checked;
alert(confirmDeleteFile:+ secured);
if(secured){
var agree = confirm(您确定要继续吗?);
if(agree){
//document.form1.submit();
返回true;
}
else
return false;
}
// submitForm();
返回true;
}
function deleteFile(){
alert('inside deleteFile()but outside window.onbeforeunload');
window.onbeforeunload = function(){
var key = DOMAIN +'::'+ elem('userName').value;
alert('deleteFile()');
app.deleteAccount(key)
alert('Unloading!');
函数dontDeleteFile(){
alert('inside dontDeleteFile()but outside window.onbeforeunload');
window.onbeforeunload = function(){
alert(Not deletion);
函数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(有些字段需要更正:\ n+ reason);
返回false;
else {
if(!confirmDeleteFile()){
return false;
}
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,厘米);%>
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 =创建ArcotOTP失败;
function resetForm(){
var form = document.forms [0];
form.username.value ='';
form.pam.value ='';
form.pin1.value ='';
form.pin2.value ='';
}
函数validateUsername(fld){
var error =;
var illegalChars = / \W /; //允许字母,数字和下划线
if(fld.value ==){
fld.style.background ='Yellow';
error =您没有输入用户名。 $(
)else if((fld.value.length< 5)||(fld.value.length> 15)){
fld.style.background ='Yellow';
error =用户名应该包含4个以上的字符。
} else if(illegalChars.test(fld.value)){
fld.style.background ='Yellow';
error =用户名包含非法字符。
} else {
fld.style.background ='White';
}
返回错误;
函数validateEmpty(fld){
var error =;
if(fld.value.length == 0){
fld.style.background ='Yellow';
error =您没有输入个人保证信息\\\
} else {
fld.style.background ='White';
}
返回错误;
函数validatePin(pin1,pin2){
var error =;
if(pin1.value!= pin2.value){
pin1.style.background ='Yellow';
pin2.style.background ='黄色';
错误+ =引脚数字不匹配\ n;
//警报(Pin数字不匹配);
}
返回错误;
$ b函数validatePinMatch(pin1,pin2){
var error =;
if(pin1.value ==){
//elem('otp').style.background ='Yellow';
pin1.style.background ='黄色';
错误+ =引脚号不能为空\;
//警报(引脚号不能为空);
$ b if(pin2.value ==){
//elem('otp').style.background ='Yellow';
pin2.style.background ='黄色';
错误+ =确认引脚号不能为空\;
//警报(引脚号不能为空);
}
返回错误;
}
< / script>
也许这是什么原因导致你的问题部分。此外,页面上的例子可能更适合跨浏览器兼容性?
window.onbeforeunload = function(e){
var e = e || window.event;
//对于版本4之前的IE和Firefox
if(e){
e.returnValue ='Any string';
}
//对于Safari
返回'Any string';
};
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>
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';
};
这篇关于window.onbeforeunload在Firefox中的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!