html – JavaScript `localStorage` with the alternative` document.cookie` for static websites

The code

Until now, things have worked as desired and I hope that new eyes will be able to report any error or item that can be improved, a live demo is hosted and updated on GitHub Pages , and the source code is only one file. fork or clone a way….

BrowserStorage.js

BrowserStorage class {
/ **
* @this BrowserStorage
* /
constructor () {
this.supports_local_storage = this.supportsLocalStorage ();
this.supports_cookies = this.supportsCookies ();
this.storage_available = (this.supports_cookies || this.supports_local_storage)? right wrong;
}

/ **
* Note, use `this.supports_local_storage` instead in the tests.
* @returns {boolean}
* /
supportsLocalStorage () {
try {
let key = & # 39; test & # 39;
let value = true;
localStorage.setItem (key, value);
localStorage.getItem (key);
localStorage.removeItem (key);
} catch (e) {
returns false;
}
return true;
}

/ **
* Note, use `this.supports_cookies` instead in the tests.
* @returns {boolean}
* /
supportsCookies () {
if (navigator.cookieEnabled) returns true;

try {
document.cookie = & # 39; testcookie & # 39 ;;
} catch (e) {
returns false;
} finally {
if (document.cookie.indexOf (& # 39; testcookie & # 39;)! = -1) {
this.remove (& # 39; testcookie & # 39;);
return true;
}
returns false;
}
}

/ **
* Copy of the `constructor` method which should not come from a type error
* /
constructorRefresh () {
this.supports_local_storage = this.supportsLocalStorage ();
this.supports_cookies = this.supportsCookies ();
this.storage_available = (this.supports_cookies || this.supports_local_storage)? right wrong;
}

/ **
* Gets the specified value of browser storage and URI components when returning
* @returns {? boolean |? number |? string}
* @this BrowserStorage
* /
Get the key) {
let value = null;
if (this.supports_local_storage) {
value = localStorage.getItem (key);
} else if (this.supports_cookies) {
leave cookie_data = document.cookie.match (& # 39; (^ |;)? + key + & # 39; = ([^;]*) (; | $) & # 39;)
value = cookie_data? cookie_data[2] : no;
}
returns encodeURIComponent (value);
}

/ **
* Removes the value selected by key from the browser storage; note for cookies, _full whipe_ will be displayed when loading the next page
* @returns {boolean}
* @this BrowserStorage
* /
remove (key) {
if (this.supports_local_storage) {
localStorage.removeItem (key)
return true;
} else if (this.supports_cookies) {
// Note, how to disable and expire in the past
// to delete a cookie the next time the page loads.
this.set (key, -7);
return true;
}
returns false;
}

/ **
* Stores the client settings in the browser, _serverless_ in the real sense
* key @peram {string | number} - _name variable_to store the value under
* value @peram {boolean | number | string} - stored under localStorage or as a cookie
* @peram {number} days_to_live - how long is a browser suggested to keep cookies
* @returns {boolean}
* @this BrowserStorage
* /
set (key, value, days_to_live = false) {
if (this.supports_local_storage) {
localStorage.setItem (key, value);
return true;
} else if (this.supports_cookies) {
let expire = & # 39 ;;

if (days_to_live) {
let date = new Date ();
if (days_to_live == 0) {
date.setTime (date.getTime ());
} other {
date.setTime (date.getTime () + (days_to_live * 24 * 60 * 60 * 1000));
}
expires = & # 39 ;; expires = + date.toGMTString ();
}

document.cookie = name + & # 39; = & # 39; + value + expires + & # 39 ;; path = / & # 39 ;;
return true;
}
returns false;
}

/ **
* Clear ** all ** LocalStorage client or cookie settings
* @returns {boolean}
* @this BrowserStorage
* /
clear () {
if (this.supports_local_storage) {
localStorage.clear ();
return true;
} else if (this.supports_cookies) {
leave the cookies = document.cookie.split (& # 39 ;;);
for (ie i = 0; i <cookies.length; i ++) {
leave cookie = cookies[i];
let key = encodeURIComponent (cookie.split (& # 39; = & # 39;)[0].replace (/ (^  s + |  s + $) / g, & # 39;));
if (typeof (key) == <string> || typeof (key) == # number;) {
this.remove (key);
} other {
returns false;
}
}
return true;
}
returns false;
}

}

main.css

/ **
* 1. Central element
* 2. Re-justify the text
* /
body {
text-align: center; / * 1 * /
}

.container__main_content {
maximum width: 80%;
maximum width: 80vw;
display: inline-block; / * 1 * /
text-align: left; / * 2 * /
}

/ **
* Note on confidentiality
* /
.container__privacy_notice {
position: fixed;
bottom: 5px;
maximum width: 80%;
maximum width: 80vw;
background color: light gray;
text-align: left; / * 2 * /
}

.privacy_notice__label {
background color: black;
White color;
font size: 1.5rem;
upholstery: 5px 10px;
Float on the right;
}

.privacy_notice__label: hover {
color: light gray;
}

/ * Status of the label when the check box is unchecked * /
.privacy_notice__label :: after {
content: attr (value);
}


/ **
* Tweaks
* /
.hidden__input {
display: none;
visibility: hidden;
opacity: 0;
filter: alpha (opacity = 0);
}


/ * Status updates when the check box is checked * /
.privacy_notice__checkbox: verified ~ .container__privacy_notice {
box-shadow: not defined;
max-width: 1.5rem;
maximum height: 1.5rem;
}

.privacy_notice__checkbox: checked ~ .container__privacy_notice * {
visibility: hidden;
opacity: 0;
filter: alpha (opacity = 0);
}

.privacy_notice__checkbox: verified ~ .container__privacy_notice .privacy_notice__label :: after {
content: attr (verified);
}

/ * Status of the label when the checkbox is checked * /
.privacy_notice__checkbox: verified ~ .container__privacy_notice :: after {
content: & # 39;  1F56D & # 39;
position: absolute;
bottom: 5px;
left: 5px;
}

/ * List of verified and hovering updates * /
.privacy_notice__checkbox: verified ~ .container__privacy_notice: hover {
maximum width: 80%;
maximum width: 80vw;
maximum height: not defined;
}

.privacy_notice__checkbox: checked ~ .container__privacy_notice: hover * {
visibility: visible;
opacity: 1;
filter: alpha (opacity = 100);
}

index.html



  
    Example of BrowserStorage
    
    

    
    

    
  

  
    

It is a static site that gives the illusion of remembering a customer. Test by accepting privacy notice then refreshing or close and reopen a tab pointing to the current URL.

Ctrl R or Ctrl shift R will refresh most desktop browsers.

Then reject the privacy statement by clicking the Acceptable button again to reverse the behavior of this site.

use Ctrl shift I open the debugger on a desktop browser, and then go to the tab / pane Storage to observe JavaScript does things for the actions mentioned above.

This site uses JavaScript, and local storage or cookies from your browser, to improve your experience here, however, this site is currently doing do not store data about your server-side activity.

Review the Privacy and security notice concerning the BrowserStorage.js , for more details on using storage and client-side scripts.

Note, I have tried using the Web Code option when writing this question, but this has caused various errors.

Questions

My goal is to have 100% coverage for browsers using JavaScript and local storage or document.cookie storage options;

  • in this day n age is it useful to use cookies for local backup storage?

  • is it useful to use the old syntax to get the OO behavior of JavaScript or use the class and a function keywords are enough?

if there are good arguments for not using class, please include feature detection examples to load another version.

  • I keep running through nper but it seems to be more focused on node-related JavaScript, should I consider issuing npm init. or would it be pollute their parcel management system?

If such projects are a good candidate for nper, then some tips on agnostic publishing methods of the operating systems Git the orders would be excellent.

  • I am currently using encodeURIComponent when returning values ​​stored in the browser, however, this leads to unpredictable behavior with Boolean and numeric values ​​(sometimes a string, sometimes not), better way to recover values?

At this point, it seems almost necessary to do a pre-check before returning to get some consistency in behavior.

  • Where did I go wrong and / or how can I improve code execution without sacrificing readability?

  • One thing I find annoying to use checkbox hacks is accessibility, are there any well ways to get Tongue and Space/Enter support?


Thank you, and I hope that even though the above is me being a bad influence that we can all learn why together.

How to receive LOCALSTORAGE data sent by AJAX on my PHP server?

That's my first question, I'm waiting for your help.

I have this code on my server to receive the data that comes from my ajax that I pull from a Localstorage storage, for this example there is only one variable that I would like to write not only in the document your_data.txt which makes file_put_contents but in the moment of retrieving the information, PHP manages them and stores them in MYsQL bd or send them to me by e-mail, help us, please 🙁

<? php
date_default_timezone_set (America / Chicago & # 39;) // CDT
echo

Server time zone: & # 39 ;. date_default_timezone_get (). & # 39;

& # 39 ;; $ current_date = date (d / m / Y == H: i: s); to print "

Server time: ". $ Current_date."

"; $ dataObject = $ _POST['data']; $ json = json_decode ($ dataObject, true); file_put_contents (& # 39; your_data.txt) & # 39 ;, $ json);

?>













enter the code here













my server receives with this PHP, it saves me only in a txt.

javascript – prevents time reset with LOCALSTORAGE

Hello.
I have a code that when you click on the button, it starts a countdown of 5 seconds, and it displays a message on the same button, that's all.
But I would like that when the page is updated, it will not restart, but that I continue counting until it reaches zero. I understand that it is possible with the local storage, I try but I still do not get anything, and I would like that one can help me with that. and if it is possible that the time passing is indicated on the same button, but if you can with local eStorage, I am very grateful to you.
Thank you for your time, my friends.

openButton male;

initial function () {
document.getElementById ("initial"). innerHTML = ".................";

openButton = setTimeout (function () {
document.getElementById ("initial"). innerHTML = "CLICK";
}, 5000, "JavaScript");
}
    

Web Application – Is LocalStorage in Web Browsers Safe enough to Store Passwords?

I am building a system where users can enter an email address and password on my login page. If they then click the registration button, they store this data in LocalStorage before redirecting the user to the registration page.

The registration page then checks LocalStorage and automatically fills in the e-mail address and password of the previous page.

My question is: how does LocalStorage store this type of data? Storing RAW passwords in this way seems like a bad idea. That being said, everything is local on the device of the user. It is not sent to any server or anything, which reduces the risk of attack.

I've also considered deleting the LocalStorage element after getting it back on the registration page. Which would make it safer.

How secure is LocalStorage for storing RAW passwords and this type of data?

localStorage – How to store the data of multiple users? [pendente]

I am working on a front project and I need to register multiple users and their correct addresses on the local storage to display them on my main page. How can I do this using only HTML, CSS and JavaScript?

xss – What is the security risk of storing an authentication token in localStorage versus cookies?

A cookie marked httpOnly, as its name indicates, is not accessible from javascript. Thus, a "conventional" web application using cookies is not vulnerable to naive XSS attacks if this flag is set (assuming no browser bug exposes it naturally). In particular, such a solution is vulnerable to CSRF; an extra effort is needed to remedy this.

In SPA frameworks, you usually need to have access to the javascript cookie because they want to put it in an authorization header and not just rely on old school-based cookie-based security. This has the advantage of being now resistant to the CSRF.

The basic reason for a SPA is that if someone manages to inject javascript, he won. You must not have XSS vulnerabilities because the attacker can change everything about your application and simply make any malicious action with the stolen token in the victim's browser. (or simply modify your application to steal the password in plain text of the user …)

As a result, people choose to tolerate the potential exposure of the session token because the supposed consequence of a successful XSS on a SPA is anyway a 100% compromise.

(In my opinion this is not at all a regulated subject, people still have heated arguments about it. There is an incalculable number of blog posts and flame wars on the forums that fight on both sides

angle – Ionic 3 LocalStorage disappears

I have an application in Ionic 1 that records information about local storage.
I have rewritten the same application on Ionic 3 with the same package ID (widget ID in config.xml), but when installing the new application on the old, I lose the localStorage, this one is empty.

If I install the old version again over the new one, I can see localStorage again, it's as if they were different applications, but they have the same package ID and j & # 39; 39, always installs on each other.

This only happens on mobile, applications do not share the same local storage. When accessing Google Devtools in the "Application" tab – "Storage" – "Local Storage", "file: //" appears when I drag the application created in Ionic 1 and "http: // localhost: 8080". created in Ionic 3.

Is there any particularity in Ionic 3 that can not see / access Ionic 1's local storage and vice versa?

javascript – Store "scroll" in LocalStorage

When the user clicks a particular button, the scroll position of an item changes. I would like this action to be stored in LocalStorage, but I do not know how to do it … can you help me?

Here is my script:

$ (& # 39; .btn.parteum & # 39;). click (function () {
$ (# Textarea #); animate ({scrollLeft: 0}, 400);
returns false;
});
$ (& # 39; .btn.parte2 & # 39;). click (function () {
$ (# Textarea #); animate ({scrollLeft: 140}, 400);
returns false;
});

jquery – Change the style / theme of the site and store it in LocalStorage

I have a theme change system on my blog, however, it has some problems, like, it can not change the style of a div.class added by jquery (addclass) or pseudo-elements (after / before / active, etc.) all disorganized !!

I am looking for a new way to proceed (OR A SOLUTION, if it exists) and I will love it if you give me suggestions …

Simplified example of my current system in JSFiddle: https://jsfiddle.net/ybocfr7n/1/

I'm showing how it works, by changing the style of the item, by recording it in local storage and indifference when trying to change the theme of the item. 39, an element: after.

Thank you so much 😉

Check if specific content exists in the localStorage

I need to check inside the beautiful ID's there is a specific number, so that I can update or add

carObjListaAnexos
(2) ["{"id":"1","src":"data:image/octet-stream;base64,/9…REBERAREQEREBERAREQEREBERAREQEREBERAREQEREH/9k="}", "{"id":"2","src":"data:image/octet-stream;base64,/9…AiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIg//Z"}"]
0: "{" id ":" 1 "," src ":" data: image / octet-stream; base64, / 9 "
1: "{" id ":" 2 "," src ":" data: image / octet-stream; base64, / 9 "
length: 2
__proto__: Table (0)