localStorage.clear() usage - javascript

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

Related

Keeping my Selections when Refreshing page

So I have looked around and there is nothing about storing data collect on a webpage so if I refresh said page it would still have everything I either wrote of selected previously. I would like to implement this feature to my website.
If I select one of the options is selected, it filters to said option but I want that option to save on my page when website is refreshed. Is there anyway to do so???
There are many ways to do that.
First of all you can save it on server, but it to much resource cost and in most situations not good solution.
For this problem the best way to store this info on client. You can use something like cookies, session storage and local storage.
I think session and local storage are the best for your purposes. Difference between them session storage save info for current sessin - browser window, loses after browser window closed. Local storage works for browser at all, loses after cockies and storage clear (by hands). So on current computer and browser local storage would save info until it directly cleared))
Also you can use query string, but it is bad solution.
You can simple find more info about localstorage in the internet. Also popular frameworks like angularjs has services to manage work with local and session storage)
The simplest way to achive what you are looking for is with LocalStorage, which is a feature that is supported on most web browsers.
The API in javascript looks something like this:
function onClick(){
localStorage.setItem('selectedRole', role);
}
And on the page load:
$( document ).ready(function() {
selectedRole = localStorage.getItem('selectedRole')
});
You should probably edit the code a bit to fit your needs, This is how I guess you implemented your application.
In your case, sessionStorage is the best choice since you need to persist on refresh only. On page close, the data will lose.
Store the data on page refresh using:
window.onbeforeunload = function () {
sessionStorage.setItem('selectedFilter', selectedFilterData);
};
Retrieve the data on page load using:
window.onload = function () {
var selectedFilter = sessionStorage.getItem('selectedFilter');
if(selectedFilter !== null) {
//set the selections
}
};

Is there any way to detect if the browsers cache is cleared

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]
}

js function to go back in browser history to last browsed page of specific domain?

I need the js function to go back in history to the last pages used in a specific domain.
reason: I send people to a subdomain to see specific content-galleries (gallery.mydomain.xyz/#1etc). I need them to return to the last page where they left of from the specific tld (mydomain.xyz/pageX) after having clicked through a number of images/videos there at subdomain...
is this possible? any ideas?
thx!
It's not possible using the built-in browser history, no, because your access to that from JavaScript is close to non-existent.
What you can do instead is save the location you want to take them back to in, say, session storage (use local storage instead if this is in a different window), and then link back to that page.
More about session storage / local storage in the web storage spec, but the short version is that it stores strings in a storage area specific to your origin, so for instance:
localStorage.setItem("last-location", "foo");
...stores "foo" in local storage for your origin, with the key "last-location". You can then use getItem to get it later when you need it:
var lastLocation = localStorage.getItem("last-location");
you could use a simple get/post variable to tell where the user is coming from and store that in a session variable for later use when the user is to be returned. As far as I know you cant access the users browsing history from the browsing client with Javascript as its a violation of the sandbox design but that may have changed recently
thx both of you for the quick answer!
... I kind of see, not being a versatile coder myself. but I get the problem involved. and see session-storage is where I want to look at then...
I will have to make this a coding job given my non-skills here :-}
but now I know what to ask for. thx again.

How to check if storing local shared object is allowed by the user or by the browser when in private browing mode?

I am developing a flex component in Flex3 which needs to store some data on the local disk for the future use using
var localData:Sharedobject=SharedObject.getLocal("localdata");
Today while i was working on this, eventually i had opened my chrome browser in incognito mode, so the data was not stored and i couldn't restore the data which i has saved earlier.
So i want to know is there any way to check if the local storage is allowed or not; using javascript. Javascript because i want to check it before even my component is loaded. Pls suggest me some ways to do it.
SharedObject.getLocal() throws an Error when it fails to create SO, so just listen for this error.
EDIT
If you need javascript solution you can create a simple Flash/Flex file which will try to create a SharedObject, then it can pass the result (true/false) to Javascript (using ExternalInterface).
Then you can pass the result to your main component...
JS can't access flash SharedObjects.
*EDIT 2 *
There is HTML5 localStorage, so you may want to use that one, but from your question I understood you wanted to check if Flash local storage works.

Is there a way to delete entire contents of localStorage in one go?

I have a web app that populates user page history into the local storage. Ahem. It should be sessionStorage, which I have now changed so that it doesn't persist.
But that left me with an interesting problem: a full localStorage to be emptied. I know you can edit the storage in the developer console to remove the data, but imagine I wanted to use localStorage for whatever reason, and at some point I wanted to completely delete the contents of data that I had placed there programmatically.
I understand local storage is domain specific so this should not be a problem - but my question is how do I globally identify my data and then delete it?
Is there a single js expression that I could use that will just wipe out my data in one hit? Or am I stuck with using naming conventions for my key and then using a reg-ex to select all the keys?
As easy as:
localStorage.clear();
Spec: http://dev.w3.org/html5/webstorage/#dom-storage-clear
localStorage.clear();
is the method call you are after see http://php-html.net/tutorials/html5-local-storage-guide/ or http://diveintohtml5.ep.io/storage.html

Categories

Resources