Accessing Device and Operating System Resources - HTML5 APPLICATIONS DEVELOPMENT MANUAL (2016)

HTML5 APPLICATIONS DEVELOPMENT MANUAL (2016)

33 - Accessing Device and Operating System Resources

The Windows Runtime environment enables developers to access in-memory resources as well as hardware using APIs.

As you learned in chapter 4, the Windows Runtime (WinRT) is the operating system environment responsible for access to devices, media, networking, local and remote storage, and other items.

The Web Storage API allows you to store data in the browser versus a server.

There are two different types of storage:

- localStorage - lets users save larger amounts of persistent data, there is no limit to how long the data persists.

- sessionStorage lets users save session state data, it only lasts for the duration of the session.

Both objects allow users to store large amounts of data without slowing down a connection because data is transferred only when requested.

localStorage

<!doctype html>

<html>

<head>

<title>localStorage Example</title>

<script type="text/javascript">

function load() {

var value = localStorage.getItem("myKey");

if (!value) {

alert("Item not found, adding to localStorage");

localStorage.setItem("myKey", "myValue");

}

else {

alert(value + " found!");

}

}

</script>

</head>

<body onload="load()">

</body>

</html>

Refresh or Reopen the file

sessionStorage

Changing each instance of localStorage to sessionStorage.

<!doctype html>

<html>

<head>

<title>localStorage Example</title>

<script type="text/javascript">

function load() {

var value = sessionStorage.getItem("myKey");

if (!value) {

alert("Item not found, adding to sessionStorage.");

sessionStorage.setItem("myKey", "myValue");

}

else {

alert(value + " found!");

}

}

</script>

</head>

<body onload="load()">

</body>

</html>

Refresh Page:

Close page and reopen:

Accessing Hardware Capabilities

Building with HTML5, CSS, and JavaScript leads to device-independent apps. Device-independent apps are able to access hardware capabilities, such as:

- Global Positioning System (GPS)

- Accelerometer

- Camera