Interaction in browser behavior when leaving the page

1

Is there any way to open a modal question, when the person tries to exit a game in the browser, type, changing the address in the URL or giving refresh, or even harder, wanting to close the browser?

I have this code, but it does not solve all problems:

$rootScope.confirmMessageInfo = function(message, callback, headerMessage, icone, buttons) {
    var titleMessage = 'Atenção',
        buttonsConfirm = {
           cancel:'Cancelar',
           ok:'Ok'
        }
    if (angular.isDefined(headerMessage) && headerMessage != null) {
        titleMessage = headerMessage;
    }
    var iconepadrao = 'atencao';
    if (angular.isDefined(icone) && icone != null) {
        iconepadrao = icone;
    }
    if (angular.isDefined(buttons)) {
        buttonsConfirm = buttons;
    }

    $scope.result_itens = {
        title:titleMessage,
        messageText:message,
        icone:iconepadrao,
        buttons:buttonsConfirm,
        id_modal:'confirm-message-info'
    };

    var size =  'modal-gerenciar alert-message-info';

    var modalInstance = $uibModal.open({
        animation: true,
        templateUrl: 'modal_confirm_info.html',
        controller: 'ModalController',
        size: size,
        resolve: {
            dados: function () {
                       return $scope.result_itens;
                   }
            }

    });
    //resultado
    modalInstance.result.then(function (selectedItem) {
        if (selectedItem.status == true) {
            callback();
        }
    }, function () {
       loadingOff('');
       $rootScope.body_modal_open = '';
       $rootScope.blurClassElement = '';
       });
    };

    $rootScope.$on('$routeChangeStart', function (e, current, pre) {

        $rootScope.isSought = false;
        $rootScope.loading_global_page = true;
        //console.log(current.$$route.templateUrl);
        if (is_running_on_android) {
            Android.onUrlChange(current.$$route.templateUrl);
        }

        if (typeof data_form != "undefined" ) {
           // console.log(game.start_game, current.$$route.templateUrl, current);
            if (game.start_game && current.$$route.templateUrl == undefined){

                var question = 'Deseja abandonar a atividade?';

                if (typeof pre.params !== 'undefined' &&
                    typeof pre.params.play !== 'undefined' &&
                           pre.params.play == 'play') {
                    question = 'Nenhuma pontuação será computada, e você perderá uma vida. Tem certeza que deseja desistir do jogo?';
                }

                $rootScope.confirmMessageInfo(question, function(){

                    if (typeof pre.params !== 'undefined' &&
                        typeof pre.params.play !== 'undefined' &&
                               pre.params.play == 'play') {
                            $rootScope.giveUp = true;
                    }

                    game.start_game = false;
                    $rootScope.autosave_autoria = false;
                    $rootScope.autosave_producao_texto = false;
                    game.played = false;
                    $rootScope.forceLeave = true;
                    $rootScope.hasChangesAutoria = false;

                    if ($rootScope.giveUp) {
                        window.location.href='/'+current.$$route.redirectTo;
                    } else {
                        $location.path(current.$$route.redirectTo);
                    }

                });

            }

            if(typeof game.atualiza_pagina !== 'undefined' && data_form.atualiza_pagina){
                game.atualiza_pagina = false;
                window.location.href = "/"+current.$$route.redirectTo;
            }


         }
        if(typeof game != "undefined" && game.start_game) {
            e.preventDefault();
        }

  });

if (!$scope.btnReturn) {
    //para reiniciar a interação do onbeforeunload
    $('body').triggerHandler('click');
}

$window.onbeforeunload = function (e) {
    if (!$scope.btnReturn && ($location.path()).indexOf('/atividade/'+type+'/play/') !== -1 && !$rootScope.giveUp) {
e = e || window.event;
e.preventDefault = true;
e.cancelBubble = true;
e.returnValue = 'Se você sair agora desta atividade, irá perder automaticamente uma tentativa!!';
    }
};

I wanted another way, so by read here , or customize more can the text.

    
asked by anonymous 11.06.2018 / 14:24

1 answer

0

You can use the onbeforeunload function of provider $window :

...

$window.onbeforeunload = function() {
  return 'mensagem!';
}

...
    
11.06.2018 / 15:37