如何防止窗口。在ajax响应中使用javascript的onbeforeunload函数

时间:2022-08-18 11:51:28

I am using java-script window.onbeforeunload function to show an alert to user whenever user tries to leave the page without submitting the data. This functionality is working fine.

我正在使用java-script窗口。onbeforeunload函数,在用户试图离开页面而不提交数据时向用户显示警报。这个功能运行良好。

I facing problem, while ajax response received from server page and placed in success callback function and then i reload that window page using window.location.href functionality

我遇到了问题,当ajax响应从服务器页面接收并放入成功回调函数,然后我使用windows .location重新加载窗口页面。href功能

window.location.href=baseurl+'index.php/functionname/'+arg_id;

window.location.href = baseurl + / ' ' index . php / functionname + arg_id;

While page reloading into another page it shown that dialog box section Here my ajax function:

当页面重新加载到另一个页面时,它显示了对话框部分我的ajax功能:

$.ajax({
 type: "POST",
 url: baseurl+"index.php/sample/samplefunction",
 data: {"demo_detail":sse,'demo_detail':pro},
 success:function(data) {
 if(data) {
    window.location.href=baseurl+'index.php/functionname/'+arg_id; 
    }
 }
}); 

Here my working window.onbeforeunload functionality

在这里我的工作窗口。onbeforeunload功能

window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "You have attempted to leave this page. Are you sure you want to exit this page?";
  }  

How to prevent for window.onbeforeunload function in ajax response using javascript

如何防止窗户。在ajax响应中使用javascript的onbeforeunload函数

2 个解决方案

#1


1  

You can turn off the handler in the AJAX callback:

您可以在AJAX回调中关闭处理程序:

 if(data) {
    window.onbeforeunload = undefined;
    window.location.href=baseurl+'index.php/functionname/'+arg_id; 
    }

#2


2  

You can use a Global variable as follows:

您可以使用以下全局变量:

var confirm = true;

In your ajax response set confirm to false as follows:

在您的ajax响应集中确认为false,如下所示:

$.ajax({
 type: "POST",
 url: baseurl+"index.php/sample/samplefunction",
 data: {"demo_detail":sse,'demo_detail':pro},
 success:function(data) {
  if(data) {
     confirm = false;
     window.location.href=baseurl+'index.php/functionname/'+arg_id; 
  }
 }
});

In your "comfirmExit()" function, heck if confirm is try, then only alert the waring to the user.

在“comfirmExit()”函数中,如果尝试确认,那么只向用户发出警告。

window.onbeforeunload = confirmExit;
function confirmExit()
{
   if(confirm)
     return "You have attempted to leave this page. Are you sure you want to exit this page?";
   else
     return;
}

#1


1  

You can turn off the handler in the AJAX callback:

您可以在AJAX回调中关闭处理程序:

 if(data) {
    window.onbeforeunload = undefined;
    window.location.href=baseurl+'index.php/functionname/'+arg_id; 
    }

#2


2  

You can use a Global variable as follows:

您可以使用以下全局变量:

var confirm = true;

In your ajax response set confirm to false as follows:

在您的ajax响应集中确认为false,如下所示:

$.ajax({
 type: "POST",
 url: baseurl+"index.php/sample/samplefunction",
 data: {"demo_detail":sse,'demo_detail':pro},
 success:function(data) {
  if(data) {
     confirm = false;
     window.location.href=baseurl+'index.php/functionname/'+arg_id; 
  }
 }
});

In your "comfirmExit()" function, heck if confirm is try, then only alert the waring to the user.

在“comfirmExit()”函数中,如果尝试确认,那么只向用户发出警告。

window.onbeforeunload = confirmExit;
function confirmExit()
{
   if(confirm)
     return "You have attempted to leave this page. Are you sure you want to exit this page?";
   else
     return;
}