首页 > 解决方案 > 删除 window.onbeforeunload 调用,因为它在 Chrome 80 中不支持,而是我想使用 fetch 和 sendBeacon

问题描述

如果用户在搜索框中搜索页面内的文本,我们主要使用 window.onbeforeunload,该文本不再保留。因此,我们希望对除 IE 之外的所有浏览器使用相同的 fetch 或 sendBeacon,而不是 window.onbeforeunload。

用于在 serach 框中搜索文本的代码片段:

<script type="text/javascript" charset="utf-8">

    var unloadCatcher = function() {
            var dateObjAjax = new Date();
            var timestamp = dateObjAjax.getTime();
            var sortColumnIndex = $('#example').dataTable().fnSettings().aaSorting[0][0];
            var pageType = "EPPatientList";
            var paginationSize = $("#example_length select option:selected").prop('value');
            var sortDirection = $('#example').dataTable().fnSettings().aaSorting[0][1];
            var searchText = "";
            if ($('#search_input').is(':visible')) {
                searchText = $('#search_input').val();
                $("#hiddenSearchText").val(searchText);
            }
            else {
                $("#hiddenSearchText").val("");
            }

            $.ajax({
                url: "<%=userPreference%>&sortColumnIndex=" + sortColumnIndex
                    + "&pageType=" + pageType
                    + "&paginationSize=" + paginationSize
                    + "&sortDirection=" + sortDirection
                    + "&time="+timestamp,
                type: "post",
                data: $("#hiddenSearchForm").serialize(),
                cache: false,
                async: false,
                success: function(data){
                }
            });

            var otable = $('#example').DataTable();
            var pageSize = otable.page.info().length;

            var expdate = new Date();

            expdate.setTime(expdate.getTime() + (60 * 60 * 1000 * 24 * 365 * 20));
            $.cookie('SJM_PT_'+userLogonCookieName+'_PageSize',pageSize,{ expires: expdate });
        }

        window.onbeforeunload = unloadCatcher;

</script>

标签: javascriptjqueryajax

解决方案


推荐阅读