...

/

Adding JavaScript and Services to Write to LocalStorage

Adding JavaScript and Services to Write to LocalStorage

Learn how to access the local storage of the browser by using JavaScript.

Writing JavaScript to access localStorage

We need to write the JavaScript functions that will read to and write from the browser’s localStorage. We do this as follows:

  1. Right-click the wwwroot folder and select the “Add, New Folder” option from the menu.
  2. Name the new folder scripts.
  3. Right-click the scripts folder and select the “Add, New Item” option from the menu.
  4. Enter “javascript” in the “Search” box.
  5. Select “JavaScript File.”
  6. Name the file bweInterop.js.
  7. Click the “Add” button.
  8. Enter the following JavaScript:
Press + to interact
var bweInterop = {};
bweInterop.setLocalStorage = function (key, data) {
localStorage.setItem(key, data);
}
bweInterop.getLocalStorage = function (key) {
return localStorage.getItem(key);
}
  1. Open the wwwroot\index.html file.
  2. Add the following reference within the body element:
Press + to interact
<script src="scripts/bweInterop.js </script>
...