Interaction in browser behavior when leaving the page

Asked

Viewed 107 times

1

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

I have this code, but it doesn’t solve all the 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();
        }

  });

Obs: I already use the method onbeforeunload, even so is the old Alert, ugly and crude, would like to do it with modal, it is possible:

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, because for what read here, nor customize more can the text.

1 answer

0

You can use the function onbeforeunload of provider $window:

...

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

...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.