SlideShare a Scribd company logo
JavaScript, Sixth Edition
Chapter 10
Programming for Touchscreens and
Mobile Devices
JavaScript, Sixth Edition 2
Objectives
When you complete this chapter, you will be able to:
• Integrate mouse, touch, and pointer events into a
web app
• Obtain and work with a user's geolocation
information
• Optimize a mobile web app to accommodate the
common constraints experienced by mobile users
JavaScript, Sixth Edition 3
Using Touch Events and Pointer
Events
• On touchscreen device without a moust
– browsers fire click event when user touches screen
• Other events don't translate neatly for touchscreens
JavaScript, Sixth Edition 4
Creating a Drag-and Drop Application
with Mouse Events
• Mouse events
– events based on actions of mouse or touchpad
Table 10-1 Mouse events
JavaScript, Sixth Edition 5
Understanding Mouse Events on a
Touchscreen Device
• On a touchscreen device
– Some web page elements respond to mouse events
– div element not considered clickable
• doesn't fire mouse events on touchscreen
– links and form elements are clickable
• browsers respond to touch
• Touch cascade
– Browser checks a touched element for an event
handler for multiple events
• Including some mouse events
JavaScript, Sixth Edition 6
Understanding Mouse Events on a
Touchscreen Device (cont'd.)
Figure 10-5 Touch cascade order
JavaScript, Sixth Edition 7
Understanding Mouse Events on a
Touchscreen Device (cont'd.)
• Touchscreen devices fire touchscreen-specific
events for some elements
– Touch events created by Apple
• Used on Apple iOS and Google Android
– Pointer events created by Microsoft
• Used on Windows Phone and Windows 8 and higher
JavaScript, Sixth Edition 8
Implementing Touch Events
• Respond to user's finger touches on a touchscreen
Table 10-2 Touch events
JavaScript, Sixth Edition 9
Implementing Touch Events (cont'd.)
• touchstart event
– analogous to mousedown mouse event
• touchmove event
– corresponds to mousemove
• touchend event
– similar to mouseup
• touchcancel event
– unique to touchscreen
JavaScript, Sixth Edition 10
Implementing Touch Events (cont'd.)
• Working with Touch Coordinates
– Mouse events can work with event properties
• clientX property = x coordinate of event
• clientY property = y coordinate of event
– Touch events support multitouch devices
• Allow for multiple touches on screen at once
• Don't support clientX or clientY properties as
direct children
• Each touch event has array properties
JavaScript, Sixth Edition 11
Implementing Touch Events (cont'd.)
Table 10-3 Array properties of touch events
JavaScript, Sixth Edition 12
Implementing Touch Events (cont'd.)
• Distinguishing Between App and Device Interaction
– Touchscreen devices use touch events for more than
one purpose
• Can interact via touch with an app
• Use touch to perform gestures
– Browser and device interactions like scrolling
– Use preventDefault() method
• Ensures that OS interface doesn't respond to events
when users interact with your app
JavaScript, Sixth Edition 13
Implementing Touch Events (cont'd.)
Figure 10-7 Multiple uses of a single touch event
JavaScript, Sixth Edition 14
Implementing Pointer Events
• Touchscreens on new types of devices
– Tablets
– Notebook computers
• Makes coding for touch and mouse events more
complicated
– Some devices support stylus input
– Some devices have trackpads
JavaScript, Sixth Edition 15
Implementing Pointer Events (cont'd.)
• Microsoft pointer events
– Aim to handle input from mouse, finger, or stylus with
each event
– Incorporate other event properties
• Pressure on screen
• Angle of stylus
• Only IE Mobile and IE 10 and later support pointer
events
• Some versions of IE do not recognize touch events
– Use mouse+touch+pointer for max. compatibility
JavaScript, Sixth Edition 16
Implementing Pointer Events (cont'd.)
Table 10-4 Pointer events
JavaScript, Sixth Edition 17
Implementing Pointer Events (cont'd.)
• Identifying pointer screen coordinates
– clientX and clientY properties like mouse events
• Stopping OS gestures
– Requires setting msTouchAction CSS property
• Set value to none
JavaScript, Sixth Edition 18
Using Programming Interfaces for
Mobile Devices
• APIs available to access information provided by
mobile device hardware
Table 10-5 Selected hardware APIs for mobile devices
JavaScript, Sixth Edition 19
Using the Geolocation API
• Provides access to user's latitude & longitude
• Accessed using geolocation property of
Navigator object
Table 10-5 Selected hardware APIs for mobile devices
JavaScript, Sixth Edition 20
Using the Geolocation API (cont'd.)
• Callbacks
– Arguments that contain/reference executable code
• getCurrentPosition() method
– Request user's position a single time
JavaScript, Sixth Edition 21
Using the Geolocation API (cont'd.)
Table 10-7 Properties passed on a successful geolocation request
JavaScript, Sixth Edition 22
Using the Geolocation API (cont'd.)
• Basic example:
navigator.geolocation.getCurrentPosition(showLocation);
function showLocation(position) {
console.log("Longitude: " + position.coords.longitude);
console.log("Latitude: " + position.coords.latitude);
}
JavaScript, Sixth Edition 23
Using the Geolocation API (cont'd.)
• Enhanced example
– Handle failed request, set 10-second timeout:
navigator.geolocation.getCurrentPosition(showLocation, fail,↵
{timeout: 10000});
function showLocation(position) {
console.log("Longitude: " + position.coords.longitude);
console.log("Latitude: " + position.coords.latitude);
}
function fail() {
var content = document.getElementById("mainParagraph");
content.innerHTML = "<p>Geolocation information not↵
available or not authorized.</p>";
JavaScript, Sixth Edition 24
Using the Geolocation API (cont'd.)
• Need to fail gracefully in older browsers
if (navigator.geolocation {
navigator.geolocation.getCurrentPosition(createDirections,↵
fail, {timeout: 10000});
} else {
fail();
}
JavaScript, Sixth Edition 25
Using the Geolocation API (cont'd.)
• Need to clear geolocation history for testing
Table 10-9 Steps to clear saved geolocation history
JavaScript, Sixth Edition 26
Using the Geolocation API (cont'd.)
• Sometimes users don't notice or ignore geolocation
request
– Request neither fails or succeeds
– Any dependent code not executed
JavaScript, Sixth Edition 27
Using the Geolocation API (cont'd.)
Figure 10-10 Flowchart illustrating flow of current geolocation code
JavaScript, Sixth Edition 28
Using the Geolocation API (cont'd.)
• Can handle lack of yes/no response from user
– setTimeout() method
– Start countdown before request
• If timeout expires, stop waiting and trigger failure code
• If user responds, cancel timeout
JavaScript, Sixth Edition 29
Using the Geolocation API (cont'd.)
Figure 10-11 Flowchart illustrating geolocation code that incorporates setTimeout()
JavaScript, Sixth Edition 30
Using the Geolocation API (cont'd.)
• Code complete to acquire geolocation information
– Then you can integrate with databases
• Using the Google Maps API
– Can display a map centered on user's location
– Can show route/directions between two locations
– Includes 2 constructors
• Map() creates a map object
var name = new google.maps.Map(element, options);
• LatLng() creates an object containing coordinates
center: new google.maps.LatLng(latitude, longitude)
JavaScript, Sixth Edition 31
Using the Geolocation API (cont'd.)
• Using the Google Maps API (cont'd.)
– Example—create new map centered on current
position with zoom of 11:
var currPosLat = position.coords.latitude;
var currPosLng = position.coords.longitude;
var mapOptions = {
center: new google.maps.LatLng(currPosLat, currPosLng),
zoom: 11
};
var map = new google.maps.Map(document.getElementById("map"),↵
mapOptions);
JavaScript, Sixth Edition 32
Using the Geolocation API (cont'd.)
• Using the Google Maps API (cont'd.)
– Can also create map centered on specified point
– Geocoding
• Converting physical address to latitude/longitude
coordinates
JavaScript, Sixth Edition 33
Using the Battery Status API
• Adds a battery property to Navigator object
Table 10-11 Events of the battery object
Table 10-10 Properties of the battery object
JavaScript, Sixth Edition 34
Using the Device Orientation API
• Provides access to changes in position and speed
– Gyroscope
• Device hardware that detects orientation in space
• deviceorientation event
– alpha, beta, and gamma coordinate properties
– Accelerometer
• Device hardware that detects changes in speed
• devicemotion event
– reports values for acceleration and rotation
JavaScript, Sixth Edition 35
Using the WebRTC API
• Short for web real-time communication
• Enables apps to
– receive data from device camera and microphone
– send and receive audio/video/data in real time
• Should eventually allow text/video chat using only
HTML and JavaScript
JavaScript, Sixth Edition 36
Enhancing Mobile Web Apps
• Testing tools
– Often impractical to collect many mobile devices
– Online services available for testing
– Free testing apps from mobile OS makers:
Table 10-12 Software used to simulate mobile devices
JavaScript, Sixth Edition 37
Enhancing Mobile Web Apps (cont'd.)
• Minimizing Download Size
– Mobile speeds usually slower
– Mobile users often have data caps
JavaScript, Sixth Edition 38
Enhancing Mobile Web Apps (cont'd.)
• Minimizing Download Size (cont'd.)
– Loading Scripts Responsively
Figure 10-14 Implementing responsive script loading for oaktop.htm
JavaScript, Sixth Edition 39
Enhancing Mobile Web Apps (cont'd.)
• Minifying Files
– Removes comments, indents, and line breaks
– Tweaks code in other ways to make it smaller
– Online minifying services available
JavaScript, Sixth Edition 40
Summary
• Touch events focus on responding to a user's finger
touches on a touchscreen
• To ensure OS interface doesn't respond to gestures
– Use the preventDefault() method
• Pointer events are different than touch events
– Aim to handle input from mouse, finger, or stylus
• Geolocation API provides access to a user's latitude
and longitude coordinates
• A number of tools exist for testing mobile web apps
virtually
JavaScript, Sixth Edition 41
Summary (cont'd.)
• Important to minimize download size of mobile web
app
– Load scripts responsively
– Minify files

More Related Content

What's hot

ReactJs presentation
ReactJs presentationReactJs presentation
ReactJs presentation
nishasowdri
 
Designing applications with Redux
Designing applications with ReduxDesigning applications with Redux
Designing applications with Redux
Fernando Daciuk
 
React for Dummies
React for DummiesReact for Dummies
React for Dummies
Mitch Chen
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy steps
prince Loffar
 
Building React Applications with Redux
Building React Applications with ReduxBuilding React Applications with Redux
Building React Applications with Redux
FITC
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
All Things Open
 
React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overview
Alex Bachuk
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
Sasidhar Kothuru
 
Android training day 5
Android training day 5Android training day 5
Android training day 5
Vivek Bhusal
 
Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805LearningTech
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.js
Doug Neiner
 
React render props
React render propsReact render props
React render props
Saikat Samanta
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
Varun Raj
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
Harvard Web Working Group
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
Adnan Sohail
 
Java script tutorial
Java script tutorialJava script tutorial
Java script tutorial
Doeun KOCH
 
Functions and Objects in JavaScript
Functions and Objects in JavaScript Functions and Objects in JavaScript
Functions and Objects in JavaScript Dhananjay Kumar
 
State Models for React with Redux
State Models for React with ReduxState Models for React with Redux
State Models for React with Redux
Stephan Schmidt
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and Redux
Glib Kechyn
 
The Road To Redux
The Road To ReduxThe Road To Redux
The Road To Redux
Jeffrey Sanchez
 

What's hot (20)

ReactJs presentation
ReactJs presentationReactJs presentation
ReactJs presentation
 
Designing applications with Redux
Designing applications with ReduxDesigning applications with Redux
Designing applications with Redux
 
React for Dummies
React for DummiesReact for Dummies
React for Dummies
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy steps
 
Building React Applications with Redux
Building React Applications with ReduxBuilding React Applications with Redux
Building React Applications with Redux
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
 
React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overview
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
Android training day 5
Android training day 5Android training day 5
Android training day 5
 
Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.js
 
React render props
React render propsReact render props
React render props
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
 
Java script tutorial
Java script tutorialJava script tutorial
Java script tutorial
 
Functions and Objects in JavaScript
Functions and Objects in JavaScript Functions and Objects in JavaScript
Functions and Objects in JavaScript
 
State Models for React with Redux
State Models for React with ReduxState Models for React with Redux
State Models for React with Redux
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and Redux
 
The Road To Redux
The Road To ReduxThe Road To Redux
The Road To Redux
 

Similar to 9781305078444 ppt ch10

Ch10 - Programming for Touchscreens and Mobile Devices
Ch10 - Programming for Touchscreens and Mobile DevicesCh10 - Programming for Touchscreens and Mobile Devices
Ch10 - Programming for Touchscreens and Mobile Devices
dcomfort6819
 
Location Based Services Without the Cocoa
Location Based Services Without the CocoaLocation Based Services Without the Cocoa
Location Based Services Without the Cocoa
EDINA, University of Edinburgh
 
Windows phone 7 series
Windows phone 7 seriesWindows phone 7 series
Windows phone 7 seriesopenbala
 
MTA java script coding for the touch interface
MTA  java script coding for the touch interfaceMTA  java script coding for the touch interface
MTA java script coding for the touch interface
Dhairya Joshi
 
Advance ui development and design
Advance ui  development and design Advance ui  development and design
Advance ui development and design
Rakesh Jha
 
openMIC barcamp 11.02.2010
openMIC barcamp 11.02.2010openMIC barcamp 11.02.2010
openMIC barcamp 11.02.2010Patrick Lauke
 
Developing Rich Interfaces in JavaFX for Ultrabooks
Developing Rich Interfaces in JavaFX for UltrabooksDeveloping Rich Interfaces in JavaFX for Ultrabooks
Developing Rich Interfaces in JavaFX for UltrabooksFelipe Pedroso
 
Android App development and test environment, Understaing android app structure
Android App development and test environment, Understaing android app structureAndroid App development and test environment, Understaing android app structure
Android App development and test environment, Understaing android app structure
Vijay Rastogi
 
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
mharkus
 
Lecture 12 - Maps, AR_VR_aaaaHardware.pptx
Lecture 12 - Maps, AR_VR_aaaaHardware.pptxLecture 12 - Maps, AR_VR_aaaaHardware.pptx
Lecture 12 - Maps, AR_VR_aaaaHardware.pptx
NgLQun
 
Measuring Continuity
Measuring ContinuityMeasuring Continuity
Measuring Continuity
Nicholas Jansma
 
Bucruiser ppt
Bucruiser pptBucruiser ppt
Bucruiser ppt
Vishnu Viswanath
 
iOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkiOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections Talk
Miguel de Icaza
 
Firefox OS Presentation
Firefox OS PresentationFirefox OS Presentation
Firefox OS Presentation
José Manuel Cantera Fonseca
 
Android application development
Android application developmentAndroid application development
Android application development
Md. Mujahid Islam
 
V Legakis Presentation
V Legakis PresentationV Legakis Presentation
V Legakis Presentation
VLegakis
 
amis-adf-enterprise-mobility
amis-adf-enterprise-mobilityamis-adf-enterprise-mobility
amis-adf-enterprise-mobility
Luc Bors
 
2.5 gui
2.5 gui2.5 gui
2.5 gui
Jyothi Vbs
 
HTML5 WebWorks
HTML5 WebWorksHTML5 WebWorks
HTML5 WebWorks
Raul Jimenez
 
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...Esri Nederland
 

Similar to 9781305078444 ppt ch10 (20)

Ch10 - Programming for Touchscreens and Mobile Devices
Ch10 - Programming for Touchscreens and Mobile DevicesCh10 - Programming for Touchscreens and Mobile Devices
Ch10 - Programming for Touchscreens and Mobile Devices
 
Location Based Services Without the Cocoa
Location Based Services Without the CocoaLocation Based Services Without the Cocoa
Location Based Services Without the Cocoa
 
Windows phone 7 series
Windows phone 7 seriesWindows phone 7 series
Windows phone 7 series
 
MTA java script coding for the touch interface
MTA  java script coding for the touch interfaceMTA  java script coding for the touch interface
MTA java script coding for the touch interface
 
Advance ui development and design
Advance ui  development and design Advance ui  development and design
Advance ui development and design
 
openMIC barcamp 11.02.2010
openMIC barcamp 11.02.2010openMIC barcamp 11.02.2010
openMIC barcamp 11.02.2010
 
Developing Rich Interfaces in JavaFX for Ultrabooks
Developing Rich Interfaces in JavaFX for UltrabooksDeveloping Rich Interfaces in JavaFX for Ultrabooks
Developing Rich Interfaces in JavaFX for Ultrabooks
 
Android App development and test environment, Understaing android app structure
Android App development and test environment, Understaing android app structureAndroid App development and test environment, Understaing android app structure
Android App development and test environment, Understaing android app structure
 
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
 
Lecture 12 - Maps, AR_VR_aaaaHardware.pptx
Lecture 12 - Maps, AR_VR_aaaaHardware.pptxLecture 12 - Maps, AR_VR_aaaaHardware.pptx
Lecture 12 - Maps, AR_VR_aaaaHardware.pptx
 
Measuring Continuity
Measuring ContinuityMeasuring Continuity
Measuring Continuity
 
Bucruiser ppt
Bucruiser pptBucruiser ppt
Bucruiser ppt
 
iOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkiOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections Talk
 
Firefox OS Presentation
Firefox OS PresentationFirefox OS Presentation
Firefox OS Presentation
 
Android application development
Android application developmentAndroid application development
Android application development
 
V Legakis Presentation
V Legakis PresentationV Legakis Presentation
V Legakis Presentation
 
amis-adf-enterprise-mobility
amis-adf-enterprise-mobilityamis-adf-enterprise-mobility
amis-adf-enterprise-mobility
 
2.5 gui
2.5 gui2.5 gui
2.5 gui
 
HTML5 WebWorks
HTML5 WebWorksHTML5 WebWorks
HTML5 WebWorks
 
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
 

More from Terry Yoast

9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
Terry Yoast
 
9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10
Terry Yoast
 
9781337102087 ppt ch09
9781337102087 ppt ch099781337102087 ppt ch09
9781337102087 ppt ch09
Terry Yoast
 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08
Terry Yoast
 

More from Terry Yoast (20)

9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 
9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10
 
9781337102087 ppt ch09
9781337102087 ppt ch099781337102087 ppt ch09
9781337102087 ppt ch09
 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08
 

Recently uploaded

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

9781305078444 ppt ch10

  • 1. JavaScript, Sixth Edition Chapter 10 Programming for Touchscreens and Mobile Devices
  • 2. JavaScript, Sixth Edition 2 Objectives When you complete this chapter, you will be able to: • Integrate mouse, touch, and pointer events into a web app • Obtain and work with a user's geolocation information • Optimize a mobile web app to accommodate the common constraints experienced by mobile users
  • 3. JavaScript, Sixth Edition 3 Using Touch Events and Pointer Events • On touchscreen device without a moust – browsers fire click event when user touches screen • Other events don't translate neatly for touchscreens
  • 4. JavaScript, Sixth Edition 4 Creating a Drag-and Drop Application with Mouse Events • Mouse events – events based on actions of mouse or touchpad Table 10-1 Mouse events
  • 5. JavaScript, Sixth Edition 5 Understanding Mouse Events on a Touchscreen Device • On a touchscreen device – Some web page elements respond to mouse events – div element not considered clickable • doesn't fire mouse events on touchscreen – links and form elements are clickable • browsers respond to touch • Touch cascade – Browser checks a touched element for an event handler for multiple events • Including some mouse events
  • 6. JavaScript, Sixth Edition 6 Understanding Mouse Events on a Touchscreen Device (cont'd.) Figure 10-5 Touch cascade order
  • 7. JavaScript, Sixth Edition 7 Understanding Mouse Events on a Touchscreen Device (cont'd.) • Touchscreen devices fire touchscreen-specific events for some elements – Touch events created by Apple • Used on Apple iOS and Google Android – Pointer events created by Microsoft • Used on Windows Phone and Windows 8 and higher
  • 8. JavaScript, Sixth Edition 8 Implementing Touch Events • Respond to user's finger touches on a touchscreen Table 10-2 Touch events
  • 9. JavaScript, Sixth Edition 9 Implementing Touch Events (cont'd.) • touchstart event – analogous to mousedown mouse event • touchmove event – corresponds to mousemove • touchend event – similar to mouseup • touchcancel event – unique to touchscreen
  • 10. JavaScript, Sixth Edition 10 Implementing Touch Events (cont'd.) • Working with Touch Coordinates – Mouse events can work with event properties • clientX property = x coordinate of event • clientY property = y coordinate of event – Touch events support multitouch devices • Allow for multiple touches on screen at once • Don't support clientX or clientY properties as direct children • Each touch event has array properties
  • 11. JavaScript, Sixth Edition 11 Implementing Touch Events (cont'd.) Table 10-3 Array properties of touch events
  • 12. JavaScript, Sixth Edition 12 Implementing Touch Events (cont'd.) • Distinguishing Between App and Device Interaction – Touchscreen devices use touch events for more than one purpose • Can interact via touch with an app • Use touch to perform gestures – Browser and device interactions like scrolling – Use preventDefault() method • Ensures that OS interface doesn't respond to events when users interact with your app
  • 13. JavaScript, Sixth Edition 13 Implementing Touch Events (cont'd.) Figure 10-7 Multiple uses of a single touch event
  • 14. JavaScript, Sixth Edition 14 Implementing Pointer Events • Touchscreens on new types of devices – Tablets – Notebook computers • Makes coding for touch and mouse events more complicated – Some devices support stylus input – Some devices have trackpads
  • 15. JavaScript, Sixth Edition 15 Implementing Pointer Events (cont'd.) • Microsoft pointer events – Aim to handle input from mouse, finger, or stylus with each event – Incorporate other event properties • Pressure on screen • Angle of stylus • Only IE Mobile and IE 10 and later support pointer events • Some versions of IE do not recognize touch events – Use mouse+touch+pointer for max. compatibility
  • 16. JavaScript, Sixth Edition 16 Implementing Pointer Events (cont'd.) Table 10-4 Pointer events
  • 17. JavaScript, Sixth Edition 17 Implementing Pointer Events (cont'd.) • Identifying pointer screen coordinates – clientX and clientY properties like mouse events • Stopping OS gestures – Requires setting msTouchAction CSS property • Set value to none
  • 18. JavaScript, Sixth Edition 18 Using Programming Interfaces for Mobile Devices • APIs available to access information provided by mobile device hardware Table 10-5 Selected hardware APIs for mobile devices
  • 19. JavaScript, Sixth Edition 19 Using the Geolocation API • Provides access to user's latitude & longitude • Accessed using geolocation property of Navigator object Table 10-5 Selected hardware APIs for mobile devices
  • 20. JavaScript, Sixth Edition 20 Using the Geolocation API (cont'd.) • Callbacks – Arguments that contain/reference executable code • getCurrentPosition() method – Request user's position a single time
  • 21. JavaScript, Sixth Edition 21 Using the Geolocation API (cont'd.) Table 10-7 Properties passed on a successful geolocation request
  • 22. JavaScript, Sixth Edition 22 Using the Geolocation API (cont'd.) • Basic example: navigator.geolocation.getCurrentPosition(showLocation); function showLocation(position) { console.log("Longitude: " + position.coords.longitude); console.log("Latitude: " + position.coords.latitude); }
  • 23. JavaScript, Sixth Edition 23 Using the Geolocation API (cont'd.) • Enhanced example – Handle failed request, set 10-second timeout: navigator.geolocation.getCurrentPosition(showLocation, fail,↵ {timeout: 10000}); function showLocation(position) { console.log("Longitude: " + position.coords.longitude); console.log("Latitude: " + position.coords.latitude); } function fail() { var content = document.getElementById("mainParagraph"); content.innerHTML = "<p>Geolocation information not↵ available or not authorized.</p>";
  • 24. JavaScript, Sixth Edition 24 Using the Geolocation API (cont'd.) • Need to fail gracefully in older browsers if (navigator.geolocation { navigator.geolocation.getCurrentPosition(createDirections,↵ fail, {timeout: 10000}); } else { fail(); }
  • 25. JavaScript, Sixth Edition 25 Using the Geolocation API (cont'd.) • Need to clear geolocation history for testing Table 10-9 Steps to clear saved geolocation history
  • 26. JavaScript, Sixth Edition 26 Using the Geolocation API (cont'd.) • Sometimes users don't notice or ignore geolocation request – Request neither fails or succeeds – Any dependent code not executed
  • 27. JavaScript, Sixth Edition 27 Using the Geolocation API (cont'd.) Figure 10-10 Flowchart illustrating flow of current geolocation code
  • 28. JavaScript, Sixth Edition 28 Using the Geolocation API (cont'd.) • Can handle lack of yes/no response from user – setTimeout() method – Start countdown before request • If timeout expires, stop waiting and trigger failure code • If user responds, cancel timeout
  • 29. JavaScript, Sixth Edition 29 Using the Geolocation API (cont'd.) Figure 10-11 Flowchart illustrating geolocation code that incorporates setTimeout()
  • 30. JavaScript, Sixth Edition 30 Using the Geolocation API (cont'd.) • Code complete to acquire geolocation information – Then you can integrate with databases • Using the Google Maps API – Can display a map centered on user's location – Can show route/directions between two locations – Includes 2 constructors • Map() creates a map object var name = new google.maps.Map(element, options); • LatLng() creates an object containing coordinates center: new google.maps.LatLng(latitude, longitude)
  • 31. JavaScript, Sixth Edition 31 Using the Geolocation API (cont'd.) • Using the Google Maps API (cont'd.) – Example—create new map centered on current position with zoom of 11: var currPosLat = position.coords.latitude; var currPosLng = position.coords.longitude; var mapOptions = { center: new google.maps.LatLng(currPosLat, currPosLng), zoom: 11 }; var map = new google.maps.Map(document.getElementById("map"),↵ mapOptions);
  • 32. JavaScript, Sixth Edition 32 Using the Geolocation API (cont'd.) • Using the Google Maps API (cont'd.) – Can also create map centered on specified point – Geocoding • Converting physical address to latitude/longitude coordinates
  • 33. JavaScript, Sixth Edition 33 Using the Battery Status API • Adds a battery property to Navigator object Table 10-11 Events of the battery object Table 10-10 Properties of the battery object
  • 34. JavaScript, Sixth Edition 34 Using the Device Orientation API • Provides access to changes in position and speed – Gyroscope • Device hardware that detects orientation in space • deviceorientation event – alpha, beta, and gamma coordinate properties – Accelerometer • Device hardware that detects changes in speed • devicemotion event – reports values for acceleration and rotation
  • 35. JavaScript, Sixth Edition 35 Using the WebRTC API • Short for web real-time communication • Enables apps to – receive data from device camera and microphone – send and receive audio/video/data in real time • Should eventually allow text/video chat using only HTML and JavaScript
  • 36. JavaScript, Sixth Edition 36 Enhancing Mobile Web Apps • Testing tools – Often impractical to collect many mobile devices – Online services available for testing – Free testing apps from mobile OS makers: Table 10-12 Software used to simulate mobile devices
  • 37. JavaScript, Sixth Edition 37 Enhancing Mobile Web Apps (cont'd.) • Minimizing Download Size – Mobile speeds usually slower – Mobile users often have data caps
  • 38. JavaScript, Sixth Edition 38 Enhancing Mobile Web Apps (cont'd.) • Minimizing Download Size (cont'd.) – Loading Scripts Responsively Figure 10-14 Implementing responsive script loading for oaktop.htm
  • 39. JavaScript, Sixth Edition 39 Enhancing Mobile Web Apps (cont'd.) • Minifying Files – Removes comments, indents, and line breaks – Tweaks code in other ways to make it smaller – Online minifying services available
  • 40. JavaScript, Sixth Edition 40 Summary • Touch events focus on responding to a user's finger touches on a touchscreen • To ensure OS interface doesn't respond to gestures – Use the preventDefault() method • Pointer events are different than touch events – Aim to handle input from mouse, finger, or stylus • Geolocation API provides access to a user's latitude and longitude coordinates • A number of tools exist for testing mobile web apps virtually
  • 41. JavaScript, Sixth Edition 41 Summary (cont'd.) • Important to minimize download size of mobile web app – Load scripts responsively – Minify files