Skip to content

Commit

Permalink
Updating my javascript examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakefolio committed May 4, 2012
1 parent 30ec605 commit 43960dd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
21 changes: 21 additions & 0 deletions javascript/geolocation.js
@@ -0,0 +1,21 @@
/*
maximumAge = Cache expiration
timeout = Time allowed to request location
enableHighAccuracy = Requests a more precise location (can cause faster battery consumption)
*/

if (navigator.geolocation) {
var currentPosition = navigator.geolocation.getCurrentPosition(function(pos) {
return [pos.coords.latitude, pos.coords.longitude];
});
var glocWatch = navigator.geolocation.watchPosition(function(pos) {
// Successfully changed position
currentPosition = [pos.coords.latitude, pos.coords.longitude];
}, function(error) {
// Error
console.log(error);
}, { enableHighAccuracy: true, maximumAge: 30000, timeout: 27000 });

} else {
// Geolocation not supported
}
31 changes: 31 additions & 0 deletions javascript/localstorage.js
@@ -0,0 +1,31 @@
/*
HTTP vs. HTTPS localstorage - store seperately (including incognito for chrome)
No default expiration.
Only support strings as values
SUPPORT: All modern browsers and IE8+
*/

// Check if localStorage is available
if (typeof window.localStorage == 'object' && window.localStorage != null) {
var store = window.localStorage;
var myObject = {}
var status = document.getElementById('status');
var newObj;

try {
store.setItem('myKey', 'value');
store['myObject'] = JSON.stringify(myObject);
} catch(e) {
if (e == QUOTA_EXCEEDED_ERR) {
console.log('ERROR: Local Storage is out of space!');
}
}

// Change status text
// status.innerText = store.getItem('myKey');
// status.innerText = store['myKey'];
status.innerText = store.myKey;

// Retrieve my Object
newobj = JSON.parse(store.myObject);
}
22 changes: 22 additions & 0 deletions javascript/query-selector.js
@@ -0,0 +1,22 @@
/*
querySelector returns only the first element
querySelector/querySelectorAll can be applied to the document or narrowed down to an element
*/
// Get all menu items
var items = document.querySelectorAll('.menu li');
// jQuery version: $('.menu li');

// Sub Items of the first menu item
var subItems = items[0].querySelectorAll('li');
// jQuery version: $('.menu li').find('li')

// Find the latest tweet
var latestTweet = document.querySelector('#latest-tweet');
// jQuery version: $('#latest-tweet').first();

for item in items {
// Log the content of element
console.log(item.innerHTML);
// Output the style object
console.log(item.style);
}

0 comments on commit 43960dd

Please sign in to comment.