Wednesday, March 19, 2014

Session HTML5

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<meta name="viewport" content="width=620">
<title>HTML5 Demo: Storage</title>
<link rel="stylesheet" href="css/html5demos.css">
<script src="js/h5utils.js"></script></head>
<body>
<section id="wrapper">
<div id="carbonads-container"><div class="carbonad"><div id="azcarbon"></div><script type="text/javascript">var z = document.createElement("script"); z.type = "text/javascript"; z.async = true; z.src = "http://engine.carbonads.com/z/14060/azcarbon_2_1_0_VERT"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s);</script></div></div>
    <header>
      <h1>Storage</h1>
    </header>
<style>
article div { 
  margin: 10px 0;
}

label {
  float: left;
  display: block;
  width: 125px;
  line-height: 32px;
}
</style>
<article>
  <section>
    <p>Values are stored on <code>keyup</code></p>
    <p>Content loaded from previous sessions:</p>
    <ul id="previous"></ul>
  </section>
  <section>
    <div>
      <label for="session">sessionStorage:</label>
      <input type="text" name="session" value="" id="session" />          
    </div>
    <div>
      <label for="local">localStorage:</label>
      <input type="text" name="local" value="" id="local" />
    </div>
    <input type="button" id="clear" value="Clear storage" />
  </section>
</article>
<script>
function getStorage(type) {
  var storage = window[type + 'Storage'],
    delta = 0,
    li = document.createElement('li');

  if (!window[type + 'Storage']) return;

  if (storage.getItem('value')) {
    delta = ((new Date()).getTime() - (new Date()).setTime(storage.getItem('timestamp'))) / 1000;
    
    li.innerHTML = type + 'Storage: ' + storage.getItem('value') + ' (last updated: ' + delta + 's ago)';
  } else {
    li.innerHTML = type + 'Storage is empty';
  }

  document.querySelector('#previous').appendChild(li);
}

getStorage('session');
getStorage('local');

addEvent(document.querySelector('#session'), 'keyup', function () {
  sessionStorage.setItem('value', this.value);
  sessionStorage.setItem('timestamp', (new Date()).getTime());
});

addEvent(document.querySelector('#local'), 'keyup', function () {
  localStorage.setItem('value', this.value);
  localStorage.setItem('timestamp', (new Date()).getTime());
});

addEvent(document.querySelector('#clear'), 'click', function () {
  sessionStorage.clear();
  localStorage.clear();
  
  document.querySelector('#previous').innerHTML = '';
  getStorage('local');
  getStorage('session');
});
</script>
<a id="html5badge" href="http://www.w3.org/html/logo/">
<img src="http://www.w3.org/html/logo/badge/html5-badge-h-connectivity-device-graphics-multimedia-performance-semantics-storage.png" width="325" height="64" alt="HTML5 Powered with Connectivity / Realtime, Device Access, Graphics, 3D & Effects, Multimedia, Performance & Integration, Semantics, and Offline & Storage" title="HTML5 Powered with Connectivity / Realtime, Device Access, Graphics, 3D & Effects, Multimedia, Performance & Integration, Semantics, and Offline & Storage">
</a>
    <footer><a href="/">HTML5 demos</a>/<a id="built" href="http://twitter.com/rem">@rem built this</a>/<a href="#view-source">view source</a></footer> </section>
<a href="http://github.com/remy/html5demos"><img style="position: absolute; top: 0; left: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_left_darkblue_121621.png" alt="Fork me on GitHub" /></a>
<script src="js/prettify.packed.js"></script>
<script>
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script>
try {
var pageTracker = _gat._getTracker("UA-1656750-18");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>

No comments:

Post a Comment