Identifying Between Refresh And Close Browser Actions

前端 未结 13 1407
孤街浪徒
孤街浪徒 2020-11-22 15:29

When we refresh the page (F5, or icon in browser), it will first trigger ONUNLOAD event. When we close the browser (X on right top icon),It will trigger ONUNLOAD event. Now

13条回答
  •  执念已碎
    2020-11-22 15:59

        $(window).bind('unload', function () {
            if (/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
                console.log('firefox delete');
                var data = { async: false };
                endSession(data);
                return null;
            }
            else {
                console.log('NON-firefox delete');
                var data = { async: true };
                endSession(data);
                return null;
            }
        });
    
        function endSession(data) {
            var id = 0
    
            if (window) { // closeed
                id=1
            }
    
            $.ajax({
                url: '/api/commonAPI/'+id+'?Action=ForceEndSession',
                type: "get",
                data: {},
                async: data.async,
                success: function () {
                    console.log('Forced End Session');
                }
            });
        }
    

    Use if (window) to determines if closed or just reload. working for me.

提交回复
热议问题