2

I'm currently testing on my local machine so the URL is as following when the user presses Sign out

// The ********** is my site name, which i don't want releasing yet http://localhost:8080/**********/logout/ 

Local storage item:

Key: tkn Value: EE8D9DA0F1D71A0BE8B80F3BA80343555655871151tk16 

the following tests i've tried:

if (localStorage.getItem('tkn')) { localStorage.clear(); } if (localStorage.getItem('tkn')) { localStorage.removeItem('tkn'); } // I've also ran these both tests in the console of the browser // and they return back 'undefined' but the item is cleared. 

However when I sign out, the user session is removed completely fine. However the localStorage doesn't seem to be cleared.

Could someone shred some light on this for me?

1

1 Answer 1

3

Instead of checking a specific property, check the length

if (localStorage.length > 0 ) { localStorage.clear(); } 

localStorage is an implementation of Storage interface and Storage has a property called length

Storage.length

Returns an integer representing the number of data items stored in the Storage object.

Sign up to request clarification or add additional context in comments.

2 Comments

@charlietfl In that case, specific items should be checked. But here OP has specifically asked how to clear localstorage
Nope! As of today, this does not work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.