Permalink
Browse files

Updating my javascript examples.

  • Loading branch information...
1 parent 30ec605 commit 43960dd14d7bf3e82cf4517e7b79e465bb365388 @jakefolio committed May 4, 2012
Showing with 74 additions and 0 deletions.
  1. +21 −0 javascript/geolocation.js
  2. +31 −0 javascript/localstorage.js
  3. +22 −0 javascript/query-selector.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
+}
@@ -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);
+}
@@ -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.