When loading from another page, I want to clear the page's cookies, but when reloading from the same page, I don't want to


Ishwar Kurwade

I'm building a new website, for this I create a page that has an array stored in cookies and I clear the array by using

$ .removeCookie(“ checkboxValues”);

I am running this function after the file is ready. I want to clear the cookies when I go to that page from another page using a link, yes that's what's going on. But my problem is that if I load the same page (using f5 or ctrl+r) the cookie is cleared and I don't want to lose that cookie. Any help would be greatly appreciated. thanks.

Hardy Mathew

You can try one of the following methods:

The easiest way I can think of is to add a timestamp to the redirected query string and read it in JavaScript. Set a cookie with this timestamp and detect if it already exists, to tell if the page is being reloaded (previously loaded with a query string).

Code:

The cookie is stored the first time the page is accessed. On refresh, check if your cookie exists, and if so, raise an alert.

function checkReload() {
  if(document.cookie.indexOf('YOURCOOKIE')==-1) {
    // cookie doesn't exist, create it now
  }
  else {
    // not first visit, so alert
    alert('You refreshed!');
  }
}

and in your body tag:

<body onload="checkReload()">

one more:

The first step is to check if there are some predefined values ​​for sessionStorage, and if there is an alert user:

if (sessionStorage.getItem("is_reloaded")) alert('Reloaded!');

The second step is to set sessionStorage to some value (eg true):

sessionStorage.setItem("is_reloaded", true);

The session value persists until the page is closed, so the session value is only valid when the page is reloaded in a new tab of the site.

Related


I want to save dynamic data when moving to another page

User 9041996 I want to save dynamic data when moving to another page. What you do is to draw a graph on the canvas. If you want to save the node and edge information on the canvas, when do you want to write it out, and what method should you use? I use Django,

I want to save dynamic data when moving to another page

User 9041996 I want to save dynamic data when moving to another page. What you do is to draw a graph on the canvas. If you want to save the node and edge information on the canvas, when do you want to write it out, and what method should you use? I use Django,

I want to open a page when a button is pressed

Mark Sullivan I am creating a function in React js that when the user clicks on login, the user will be redirected to the dashboard. handleSubmit(){ this.props.history.push('/blog-overview'); } When I click the button, the page is refreshed instead of