Localstorage Disabled in no-trace mode

Source: Internet
Author: User
Tags object key return window

In mobile Web development, often use to localstorage to cache some data, in general, we need to follow the following code to use it will not be a problem.

if (window.localstorage) {

Localstorage.setitem (' key ', ' value ');


But just so judged is not enough, now some browsers have a function called no trace browsing , as the name suggests, is the user in the process of browsing the Web page, nothing is cached, nothing record, leaving no traces.

Naturally, Localstorage has also been disabled. But at this point window.localstorage is not NULL, it is still the original Localstorage object, but the method of this object is disabled. If you simply follow the above method to judge, JS throws an exception.

So you have to use the following code to determine if the localstorage is available.

  1. var storagetest = function (storage) {
  2. if (!! Storage) {
  3. try {
  4. Storage.setitem ("Key", "value");
  5. Storage.removeitem ("key");
  6. return true;
  7. catch (e) {
  8. return false;
  9. }
  10. }else{
  11. return false;
  12. }
  13. }
  14. Storagetest (Window.localstorage);
  15. Storagetest (Window.sessionstorage);

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.