Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show message or alert after page reload?

I am using Jquery Toastr to show some success message. It is just like alert for some time.

I want to show some message after page reload. The problem is when page is reloaded all javascript is reload.

Is there any way to do this? I want to do this after ajax success.

success: function(data) {
    if (data.OperationStatus) {
        window.location.reload(); //Reload the page
        Toaster.show("The record is added"); //I want to show here.But it will never show because when page is loaded javascript is also loaded.            
    }
}

Is there any efficient way to do this?

like image 371
umer Avatar asked Jan 07 '16 05:01

umer


1 Answers

You have to do like this

$(document).ready(function(){
    //get it if Status key found
    if(localStorage.getItem("Status"))
    {
        Toaster.show("The record is added");
        localStorage.clear();
    }
});

on ajax call set local storage

success: function(data) {
if (data.OperationStatus) {
    localStorage.setItem("Status",data.OperationStatus)
    window.location.reload(); 
}

You can also do it with sessionStorage. Replace localStorage with sessionStorage in current code and whole code works with sessionStorage.

On localStorage works like cookies get data until you use localStorage.removeItem(key); or localStorage.clear();

On sessionStorage it remains till you close browser tab or sessionStorage.removeItem(key); or sessionStorage.clear();.

Check Reference for localStorage and sessionStorage use.

like image 116
Ankit Kathiriya Avatar answered Sep 17 '22 12:09

Ankit Kathiriya