Failed to detect actual page reload

I am using socket in javascript to emit data. Below is a snippet of code.

socket.on('my response', function(msg) {
    $('#log').append('<p>Received: ' + msg.data + '</p>');
    /*
    if (window.performance) {
        console.info("window.performance work fine on this browser");
    } 
    if (performance.navigation.type == 1) {
        console.info( "This page is reloaded" );
    } else {
        console.info( "This page is not reloaded");
    } */
    document.getElementById('div_id').innerHTML += msg.data + "<br>";
});

The problem is that the data that I display on the div is cleared after the page is reloaded. I want to save data and keep adding data from msg.dataeven after reloading the web page. How can i achieve this? I tried to detect a page reload with the commented part in the code snippet above, but it detects it as a page reload event every time I receive msg.

UPDATE 1

session-storage - , , . This page is reloaded , msg.data ( ). window.location.reload, .

+4
5

, , , , .. , , . css , stavm.

//store data first 
$( document ).ready(function() {
    // your code here
});
+2

, , .
javascript , .

HTML5 loacal storage Cookies, .

:
https://www.w3schools.com/html/html5_webstorage.asp https://www.w3schools.com/js/js_cookies.asp

:

$(function() {
  if(// This page is **not reloaded** ) {
   // **clear** local storage / cookies
  }

  //load / append data to **local storage / cookies**
  //populate data **from local storage / cookies**
});
+1

- . msg.data localStorage, localStorage, , ; .

0

- .

function reloadFunction() {
  //set property in local storage;
  window.location.reload();
}

$(function() {
  if( // local storage property exists) {
   // reloaded by code
   // clear local storage property;
  } else {
    // reloaded manually
  }
});
0

var , . , .

0

Source: https://habr.com/ru/post/1684723/


All Articles