嗨,我必须执行类似的操作,当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/