Lokal

.js

An easy-to-use library for local storage on Khan Academy!

How to Use

Like many other libraries, all you have to do is import a script within the head tag. For this library, just paste this into the head.

Hi

Documentation

Lokal.js is not large, but it provides a few useful functions. These are the save, load, get and clear functions.

First, save takes in an object and saves it to the local storage.

Second, load loads all the saved variables onto the global scope, or alternatively an object inputted into the function.

Third, get returns the key-value pairs saved into local storage under the project's id.

Finally, clear clears everything saved into local storage under the project's id.

In practice, a project using Lokal.js would look something like the following.

var player = {
    x: 200,
    y: 300,
    xv: 0,
    yv: 0
};

var level = 0;
var name = "TheDark";

Lokal.load();

document.addEventListener("click", () => {
    player.level++;

    Lokal.save({
        level: level,
        name: name
    });
});