Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Added cookieless fallbacks for IE6+, FF2+ #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 49 additions & 15 deletions aware.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,66 @@
by Ben Brown [email protected]
*/

var lastVisit = false;
var lastVisit = false,
setLastVisit,
getLastVisit;

/* Helpful little date helper here! */
Date.prototype.getDOY = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((this - onejan) / 86400000);
}

function setLastVisit(date) {
if (window.localStorage) {
window.localStorage.setItem('lastVisit',date);
} else {
// fix this
// set a cookie
}

/* Browser fallback stack from https://github.com/marcuswestin/store.js */
function usesLocalStorage(){
try { return !!('localStorage' in window && window.localStorage); }
catch(err){ return false }
}
function usesGlobalStorage(){
try { return !!('globalStorage' in window && window.globalStorage); }
catch(err){ return false }
}


function getLastVisit() {
if (window.localStorage) {
return window.localStorage.getItem('lastVisit');
} else {
// fix this
// return from cookie
if (usesLocalStorage()){
setLastVisit = function(date) { window.localStorage.setItem('lastVisit',date); }
getLastVisit = function() { return window.localStorage.getItem('lastVisit'); }
} else if (usesGlobalStorage()){
setLastVisit = function(date) { window.globalStorage[window.location.hostname]['lastVisit'] = date; }
getLastVisit = function(){ return window.globalStorage[window.location.hostname]['lastvisit']; }
} else if (window.document.documentElement.addBehavior){
var storageContainer,
storageOwner,
elem;
try {
storageContainer = new ActiveXObject('htmlfile');
storageContainer.open();
storageContainer.write('<s' + 'cript>document.w=window</s' + 'cript><iframe src="/favicon.ico></iframe>');
storageContainer.close();
storageOwner = storageContainer.w.frames[0].document;
elem = storageOwner.createElement('div');
} catch(e){
elem = window.document.createElement('div');
storageOwner = window.document.body;
}
setLastVisit = function(date){
storageOwner.appendChild(elem);
elem.addBehavior('#default#userData');
elem.load('localStorage');
elem.setAttribute('lastVisit', date);
elem.save('localStorage');
storageOwner.removeChild(elem);
}
getLastVisit = function(){
storageOwner.appendChild(elem);
elem.addBehavior('#default#userData');
elem.load('localStorage');
var result = elem.getAttribute('lastVisit');
storageOwner.removeChild(elem);
return result;
}
}


function pluralizeString(num,str) {
if (num==1) {
Expand Down
24 changes: 13 additions & 11 deletions demo.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>Reader Aware Design Demo</title>
<script src="http://xoxco.com/js/jquery.min.js"></script>
<script src="aware.js"></script>
<script>
$(function() {
$('.post,#links li,#featured li').aware();
$('.post').shkmark();
});
</script>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<style>
body { margin: 0; }
a:visited { color: #000; }
Expand Down Expand Up @@ -213,7 +208,14 @@
</head>

<body>

<script src="http://xoxco.com/js/jquery.min.js"></script>
<script src="aware.js"></script>
<script>
$(function() {
$('.post,#links li,#featured li').aware();
$('.post').shkmark();
});
</script>
<div style="background: #5DACE2; color: #FFF; padding: 5px; text-align: right;">
Powered by Aware.js: <a href="http://xoxco.com/projects/code/aware/">Get the code</a>
</div>
Expand Down Expand Up @@ -392,5 +394,5 @@ <h2><a href="demo.html" class="permalink">Newish post</a></h2>
</ul>

</div>

</body>
</body>
</html>