Adding Functionality Using Built-In Javascript Libraries
Adding Functionality Using Built-In Javascript Libraries
function drop(ev) {
var src = ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(src));
return false;
}
The dataTransfer Object
. dataTransfer properties
- dropEffect: used to specify which operation will be performed
Value must be value of effectAllowed values: can be all, none, copy, move, link
- files
. - types
dataTransfer methods
- setData: specifies the data to be dragged
format: mimetype
data: actual data var dragIcon = document.createElement('img');
dragIcon.src = 'logo.png';
- getData
- clearData dragIcon.width = 100;
e.dataTransfer.setDragImage(dragIcon, -10, -10);
- setDragImage
- addElement
Demo: Dragging Items into the Cart
Image courtesy of Stirling Noyes on Flickr, used under Creative Commons license
Web storage options
Once upon a Time
. Web apps only had cookies
- Sent over the line with every request
- Not secure
- Limited to 4KB
- 20 cookies per domain
- Can be disabled by the user
Not a real solution
What We Need in Terms of Storage
More space Client-side storage Not deleted on Not sent over the
refresh wire
Web Storage
. Also known as local storage or DOM storage
. Supports persistent storage on the client
- Better than cookies
- Has a real API to use
- Key/value pairs
- Data is on the device, not transferred with every request
- Created per domain
- Supported in all major browsers
- Limited to 5MB on most browsers
Web Storage
removeitem
localStorage.removeItem("coffee")
clear localStorage.clear()
Image courtesy of Stirling Noyes on Flickr, used under Creative Commons license
Geolocation
Geolocation
. Allows browser to retrieve geographic location of the user
- Latitude
- Longitude
- Height
- Speed
-
Highly accurate
. Useful for
- Weather sites
- Map sites
- Traffic information
Security Warning
. User gets warning about location tracking
- Name of tracking site is displayed
- Cant be bypassed
. User remains in control
- Can accept or deny access to location
.
User can decide if access if for one time or always per site
function getPosition() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition
(showPosition, errorCallback);
}
else {
infoParagraph.innerHTML = "Sorry, geolocation not supported";
}
}
function showPosition(position) {
infoParagraph.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function errorCallback(e) {
infoParagraph.innerHTML = "Something is wrong, we can't find
you";
Geolocation API
. watchPosition: used to listen for changes to position of the user
var watchId;
watchId = navigator.geolocation.watchPosition(showPosition);
.
clearWatch: used to stop listening for updates
navigator.geolocation.clearWatch(watchId);
Important Objects in the Geolocation API
. PositionOptions
var options = {
timeout:5000,
enableHighAccuracy: true,
maximumAge: 1000
};
watchID = navigator.geolocation.watchPosition(
showPosition,
errorCallback,
options);
Important Objects in the Geolocation API
. Position:
- used as container for results of successful location retrieval
- Exposes Coordinates
function showPosition (position) {
console.log(position.timestamp);
}
Image courtesy of Stirling Noyes on Flickr, used under Creative Commons license
Summary