Is there any way to detect if the browsers cache is cleared , I want to check this on a javascript application and I am using session storage.
Example:
I am on page 1.
i do some operations, like edit a field ..etc
i clear the cache.
Can i get to know that cache is cleared , when i am still on page 1. i use session storage.
I understand , the storage event will let us identify any modification on the session storage when the current tab is closed and new tab is opened.
but i want to set a flag or throw an alert message whenever the cache of the current browser is cleared.
Please help!
I dont know straight way, but this may help:
if (!localStorage.length && !sessionStorage.length) {
// No items in localStorage and in sessionStorage,
// cache possibly cleared
[your code here]
}
Related
I'm using the navigator.storage.persist() API on Chrome, and have managed to get it set to true. But I would like to (at least for testing) be able to clear the setting putting back to false.
The API definition does not include a method or flag to clear as far as I can tell. See https://developer.mozilla.org/en-US/docs/Web/API/StorageManager and the living standard: https://storage.spec.whatwg.org/#storagemanager
However, for my purposes it would also be acceptable if there was a way from 'Site settings', the clear cache options, or even a custom page like the chrome://appcache-internals/ page for appcache.
If not, where does the flag get stored? i.e. what would I need to delete in the file system to reset things?
I has not been able to find a way to clear the flag for a specific website.
But for testing purposes (as you requested), the flag returned by the StorageManager.persisted() can be cleared by:
navigate to chrome://settings/?search=cooki
click on Clear browsing data
select Cookies and other site data in the popup and click Clear
data button
After performing the above steps, StorageManager.persisted() starts to return false.
localStorage.clear() seems to behave in a manner that completely wipes all local storage. Local Storage seems to fit my use case, but I'm concerned of the possibility the local storage isn't guaranteed to not be cleared outside of my control.
For example:
My web site has a line of code:
localStorage.setItem('some', 'data');
Some other website that is operating in the same browser on another tab has this line of code:
localStorage.clear();
If that scenario ever happens, when I go to access localStorage.getItem('some');, it will return null because someone else ran localStorage.clear().
Should we never run localStorage.clear()?
What is the proper usage of localStorage.clear() if I am heavily
using local storage and don't want to write a ton of code to remove
items from the local storage individually
localStorage.removeItem('item1'); localStorage.removeItem('item2');
// etc...
Is there some way to prevent items from being removed if someone
runs localStorage.clear() somewhere else?
There is no need to worry, every time you open a new window or tab a new session is created and localStorage refers to the local session of that tab/window which is unique for that domain.
Your fine running it, it will only clear your variables
If you need to clear everything use feel free to use localStorage.clear()
Someone else cannot clear your local storage
https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
It is unique per domain. For example the localStorage for your domain
protocol://abc.com will be different from protocol://xyz.com
Is there a way to execute JavaScript code only once when a window or a tab in a browser is opened and then never again for the whole life-cycle of this window/tab (even when navigated away)?
One way to use window.sessionStorage like this.
if (!window.sessionStorage.getItem("isExecuted")) {
//execute code
window.sessionStorage.setItem("isExecuted", true);
}
MDN docs
The sessionStorage property allows you to access a session Storage
object. sessionStorage is similar to Window.localStorage, the only
difference is while data stored in localStorage has no expiration set,
data stored in sessionStorage gets cleared when the page session ends.
A page session lasts for as long as the browser is open and survives
over page reloads and restores. Opening a page in a new tab or window
will cause a new session to be initiated, which differs from how
session cookies work.
Here is a solution based on checking the window.history.length is 1 (i.e. the first time they've been to this window/tab). It also does a check that they've not simply refreshed the page.
if(window.history.length === 1 && performance.navigation.type !== 1){
alert("show me once per tab");
}
If you put the script you want to run once where I have my alert, it should work for what you are trying to do.
You can use localStorage.
Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.
The localStorage object stores the data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.
localStorage.getItem('key');
localStorage.setItem('key', 'value');
Doc: http://www.w3schools.com/html/html5_webstorage.asp
I have a page where a user is filling out a very large multi-page form. I'm using garlic.js to persist data into localstorage on the event of a crash or misclick of closing the browser.
What's the easiest way I can go about getting the user back to the last page they were filling out after logging back in, in the event of a crash or browser closing?
Can I use localStorage for this or cookies? Fairly new to javascript so the simplest solution would be appreciated!
In case your page does not have any pre-requisites, you can follow below steps:
On the page load, store the URL into the localStorage
As soon as any input field is changed, store its value in localStorage
Now the browser crash accidentally and you reopen your application.
From the localStorage -
Fetch the last page URL and load it into browser
As the page loaded, assign all inputs their previous values from LocalStorage.
What you could do:
Keep saving the data in localStorage every input
If for whatever reason the user browser crash and the client is disconnected, when he comes back, let him log back in, and let him access a new form.
On a new form, check if previous data exists, if so fill the inputs, if not it means that it's his first time.
I have a modal dialog window. Is there any event (or way) how to know when window is opened first time (not refresh, not postBack etc)? It is necessary for clearing cookie.
I assume you are the one coding the 'opening window' behavior. Why not store some variable (flag) that indicated whether the windows has been opened before or not? This can be a normal veriable or a session, cookie, local storage, etc. for long term storage.
If the just the cookie is your concern,
then change the cookie key(if you want to discard old cookie) or
don't set any expiration time for you cookie(if you want it not be sent with next fresh request) as default is session