Laravel5.1使用javascript

时间:2016-04-21 09:18:35

标签: jquery ajax laravel-5.1

如何使用javascript检测浏览器是否已关闭。我还没有找到任何使用laravel的解决方案。所以现在我在窗口关闭之前使用jquery ajax请求注销。为此我正在使用 -

function windowUnloaded() {

    var unloaded = false;
    $(window).on('beforeunload', unload);
    function unload(){      
        if(!unloaded){
            $('body').css('cursor','wait');
            $.ajax({
                type: 'get',
                async: false,
                url: '/logout',
                success:function(){ 
                    unloaded = true; 
                    $('body').css('cursor','default');
                },
                timeout: 5000
            });
        }
    }
}

仅当我刷新页面时才有效。但是当浏览器关闭时我想要它。有人可以告诉我我错在哪里或者是否还有其他更好的解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:0)

  

我没有找到任何使用laravel的解决方案

是什么让你这么说,我不知道。但是,我仍然会努力回答你的问题。

因此,您希望在浏览器关闭时注销用户?为什么不在expire_on_close文件中将true设置为false而不是config/session.php

在该文件的文档中明确指出:

/*
 |--------------------------------------------------------------------------
 | Session Lifetime
 |--------------------------------------------------------------------------
 |
 | Here you may specify the number of minutes that you wish the session
 | to be allowed to remain idle before it expires. If you want them
 | to immediately expire on the browser closing, set that option.
 |
 */

 'lifetime' => 120,

 'expire_on_close' => false,

expire_on_close设置为true会使浏览器关闭时的会话失效,并会自动注销用户。

希望这会帮助你。快乐的编码。欢呼声。