javascript - jQuery Ajax,在ajax进行时阻止刷新按钮

标签 javascript jquery ajax

嗨,我必须执行类似的操作,当ajax正在进行时,然后不允许用户刷新页面。

这是我的代码

$('#submit').click(function() {
        $(function() {
        $(".col1").mask("Generating csv...."); //This will generate a mark, Here i would like to prevent the user from doing any sort of operation.
        var to = $('#filters_date_to').val();
        var from = $('#filters_date_from').val();
        $.ajax({
            url:"../dailyTrade/createCsv?filters[date][to]="+to+"&filters[date][from]="+from,success:function(result){
                if(result) {
                     $(".col1").unmask(); //Here we can unlock the user from using the refresh button.
                     window.location = '../dailyTrade/forceDownload?file='+result; 
                     setTimeout('location.reload(true);',5000);
                }
            }
            });

        });
   });

任何建议。

最佳答案

您能做的最好的事情就是使用 onbeforeunload 向用户显示一条消息,说明请求正在进行中,并询问他们是否确定要继续。

例如

var req;

window.onbeforeunload = function() { 
 if(req) {
   return 'Request in progress....are you sure you want to continue?'; 
 }
};


//at some point in your code
req = //your request...

您无法以任何方式使用 JS 或其他任何方式阻止用户离开您的页面。

关于javascript - jQuery Ajax,在ajax进行时阻止刷新按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22608776/

相关文章:

jQuery 验证插件自定义方法。多个参数

php - PHP脚本输出的AJAX持续响应

ajax - Mvc 自动完成 ajax

javascript - 从嵌套对象中删除父键

javascript - 语法错误: illegal character when posting array to data attribute of highcharts

javascript - 文件上传时调用函数

javascript - jQuery Slideshow - 在一张幻灯片处于事件状态时隐藏其他幻灯片

javascript - 在 AJAX 中使用 PHP 变量

javascript - 如何在 Servoy 框架中扩展 JavaScript 原型(prototype)?

javascript - 在 jQuery(或 Javascript)中,如何获取 "topmost"元素?