Jquery cookie - Display notification only once

Asked

Viewed 153 times

1

I need a notification to be displayed as soon as the login is done, but when updating the page in the browser or F5, the notification is not displayed again.

Using the cookie, is there any way to do this?

Down with the code I got

Controller

public function obter_mensagem()
{       
    $notificacao = array();
        $notificacao['mensagem'] = 'teste';
        $notificacao['tipo'] = 1;
    echo json_encode($notificacao);
}

Javascript

console.log("master-page trabalhando");
/*** Variáveis ***/
var enum_toastr_type = { success: 1, info: 2, warning: 3, error: 4 }

/*** PageLoad Início ***/
$(document).ready(function() {	
	toastr.options = {
        closeButton: true,
        positionClass: 'toast-bottom-right',
        timeOut: '20000'
    }
    obter_mensagem_ajax();   
});
/*** PageLoad Fim ***/

/*** Métodos Início ***/
function exibe_mensagem_toastr(mensagens)
{
    $(mensagens).each(function () {

        switch (this.tipo) {
            case enum_toastr_type.info:
                toastr.info(this.mensagem);
                break;
            case enum_toastr_type.success:
                toastr.success(this.mensagem);
                break;
            case enum_toastr_type.warning:
                toastr.warning(this.mensagem);
                break;
            case enum_toastr_type.error:
                toastr.error(this.mensagem);
                break;
        }
    });
}
/*** Métodos Fim ***/

/*** Ajax Início ***/
function obter_mensagem_ajax() {
    $.ajax({
        type: 'GET',
		async: false,
        contentType: 'application/json; charset=utf-8',        
		url : "helper/obter_mensagem",
        success: (function (data) {
			//console.log(data);
			_obj = JSON.parse(data);
            exibe_mensagem_toastr(_obj);
        }),
        error: (function (erro) {
            trata_erro_ajax(erro);
        })
    });
}
/*** Ajax Fim ***/

  • If you need this only on the client side (the server does not need this information) then a better way is https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage (localStorage)because you thus avoid consuming bandwidth unnecessarily with the cookie in the requests/responses

  • @Miguel, The server will be necessary, because I will validate access, for example; how much time to access expire and notify on the screen.

  • Ha ok, so cookie is better

  • Help you? https://answall.com/a/190578/5878

  • @Andersoncarloswoss, I couldn’t adapt to my controller.

  • @Andersoncarloswoss, this server to check if the cookie exists. How do I create it using the controller I have here ?

Show 1 more comment
No answers

Browser other questions tagged

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