Ответ 1
Работа javascript вокруг
Это единственная "чистая" работа, о которой я мог подумать, и, похоже, она работает нормально.
Еще один код в вашем вопросе, показывающий, как вы на самом деле используете "onbeforeunload", будет большим.
Но я предполагаю, что вы используете что-то вроде кода ниже для анимации курсора "Загрузка...".
/* Loading Progress Cursor
*
* Tested on: IE8, IE11, Chrome 37, & Firefox 31
*
* [1] the wildcard selector is not performant but unavoidable in this case
*/
.cursor-loading-progress,
.cursor-loading-progress * { /* [1] */
cursor: progress !important;
}
Решение
1-й шаг:
/* hooking the relevant form button
* on submit we simply add a 'data-showprogresscursor' with value 'no' to the html tag
*/
$(".js-btn-download").on('submit', function(event) {
$('html').data('showprogresscursor', 'no' );
});
2-й шаг:
/* [1] when page is about to be unloaded
* [4] here we have a mechanism that allows to disable this "cursor loading..." animation on demand, this is to cover corner cases (ie. data download triggers 'beforeunload')
* [4a] default to 'yes' if attribute not present, not using true because of jQuery .data() auto casting
* [4b] reset to default value ('yes'), so default behavior restarted from then on
*/
var pageUnloadOrAjaxRequestInProgress = function() {
/* [4] */
var showprogresscursor = $('html').data('showprogresscursor') || 'yes'; /* [4a] */
if( showprogresscursor === 'yes' ){
$('html').addClass('cursor-loading-progress');
}
else {
$('html').data('showprogresscursor', 'yes' ); /* [4b] */
}
}
$( window ).on('beforeunload', function() { /* [1] */
pageUnloadOrAjaxRequestInProgress();
});
Обратите внимание, что я использую $('html').addClass('cursor-loading-progress');
, так как это ожидаемый CSS в моем примере, но вы можете просто сделать все, что вам нравится на данный момент.
Еще одна пара задач:
- Чтобы открыть страницу загрузки файла на новой вкладке
- Чтобы запустить обновление страницы после завершения загрузки файла