Mobile HTML5: Implementing a Responsive Cross-Platform Application
Total Page:16
File Type:pdf, Size:1020Kb
Aalto University School of Science Degree Programme of Computer Science and Engineering Kimmo Puputti Mobile HTML5: Implementing a Responsive Cross-Platform Application Master's Thesis Kirkkonummi, May 15, 2012 Supervisor: Professor Petri Vuorimaa, Aalto University Instructor: Risto Sarvas D.Sc.(Tech.) Aalto University School of Science ABSTRACT OF Degree Programme of Computer Science and Engineering MASTER'S THESIS Author: Kimmo Puputti Title: Mobile HTML5: Implementing a Responsive Cross-Platform Application Date: May 15, 2012 Pages: ix + 70 Professorship: Media Technology Code: T-111 Supervisor: Professor Petri Vuorimaa Instructor: Risto Sarvas D.Sc.(Tech.) In twenty years, the Web has become an integral part of our everyday lives. The rapid growth of the smartphone market has brought the Web from our home desks to anywhere we are, and enabled us to access this vast source of information at any time. However, the proliferation of mobile devices and platforms has raised new prob- lems for application development. The growing amount of different platforms and their distinct native technologies make it hard to develop applications that can be accessed with all these devices. The only combining factor in all these platforms is the browser, and it is be- coming the universal application platform. We cannot afford anymore to build applications for the silos and walled gardens of single platforms, and building cross-platform applications is essential in the modern mobile market. In this work, I introduce the HTML5 (HyperText Markup Language version 5) specification as well as several related specifications or specification drafts for modern web development. I also present several tools and libraries for mobile web development. I implemented a mobile web application and a network utility library, and assessed the practical performance of the modern tools and APIs (Application Program- ming Interface). In this work, I present the tools and techniques for performance optimization of mobile web applications. Keywords: mobile, HTML5, cross-platform, performance Language: English ii Aalto-yliopisto Perustieteiden korkeakoulu DIPLOMITYON¨ Tietotekniikan tutkinto-ohjelma TIIVISTELMA¨ Tekij¨a: Kimmo Puputti Ty¨on nimi: Mobiili HTML5: Suorituskykyisen ja alustariippumattoman sovelluksen toteutus P¨aiv¨ays: 15 toukokuuta 2012 Sivum¨a¨ar¨a: ix + 70 Professuuri: Mediatekniikka Koodi: T-111 Valvoja: Professori Petri Vuorimaa Ohjaaja: Tohtori Risto Sarvas Kahdenkymmenen vuoden aikana webist¨a on tullut oleellinen osa jokap¨aiv¨aist¨a el¨am¨a¨amme. Mobiilimarkkinoiden huikea kasvu on tuonut webin kotip¨oydilt¨amme mukaamme miss¨a ikin¨a olemmekin ja mahdollistanut t¨am¨an laajan tietovaraston k¨aytt¨amisen milloin tahansa. Mobiililaitteiden k¨ayt¨on r¨aj¨ahdysm¨ainen kasvu on kuitenkin nostanut uusia haasteita ohjelmistokehitykselle. Monien eri alustojen natiiviteknologiat poikkea- vat toisistaan, ja ohjelmistojen kehitt¨aminen kaikille n¨aille alustoille on haastavaa. Ainoa yhteinen tekij¨a n¨aiss¨a alustoissa on WWW-selain (World Wide Web), jos- ta on tulossa universaali ohjelmistoalusta. En¨a¨a ei voida kehitt¨a¨a ohjelmistoja vain tiettyjen suljettujen alustojen k¨aytt¨ajille, ja alusta-riippumattomuudesta on tullut oleellinen osa mobiilimarkkinoita. T¨ass¨a ty¨oss¨a esittelemme HTML5-standardin sek¨a muita siihen liittyvi¨a stan- dardeja sek¨a standardiluonnoksia, jotka tuovat uusia ominaisuuksia ja helpotuk- sia web-kehitykseen. Esittelemme my¨os useita ty¨okaluja ja tekniikoita moderniin web-kehitykseen mobiililaitteille. Toteutimme mobiililaitteissa toimivan web-ohjelmiston sek¨a kirjaston tiedon si- irt¨amiseen mobiiliverkoissa, ja arvioimme modernien ty¨okalujen ja rajapintojen k¨ayt¨ann¨on suorituskyky¨a. T¨ass¨a ty¨oss¨a esit¨amme useita ty¨okaluja ja tekniikoita web-ohjelmistojen suorituskyvyn optimointiin mobiililaitteille. Asiasanat: mobiili, HTML5, alusta-riippumattomuus, suorituskyky Kieli: Englanti iii Acknowledgements I would like to thank my supervisor, Professor Petri Vuorimaa, for the valu- able comments and instructions for this work; my instructor, Doctor Risto Sarvas, for giving me a valuable point of view to see this work in the proper context as a whole; and my colleague, Jarno Rantanen, for making the demo application for the utility library documented in this work. I would also like to thank my employer, Futurice, for giving me time to write this work and to do satisfying projects, such as the practical part of this work, every day with different customers. Mostly, I would like to thank my wife Maija for the valuable support and my firstborn son Leo who was born during the writing of this work. You are the light of my life. Kirkkonummi, May 15, 2012 Kimmo Puputti iv Abbreviations and Acronyms 3G 3rd Generation Mobile Telecommunications API Application Programming Interface Ajax Asynchronous JavaScript and XML AppCache Application Cache Blob Binary Large Object CDN Content Delivery Network CORS Cross-Origin Resource Sharing CPU Central Processing Unit CSS Cascading Style Sheets CSS3 Cascading Style Sheets level 3 DNS Domain Name System DOM Document Object Model ETag Entity Tag GPS Global Positioning System HTML HyperText Markup Language HTML5 HyperText Markup Language version 5 HTTP Hypertext Transfer Protocol HTTPS Hypertext Transfer Protocol Secure IP Internet Protocol IT Information Technology JSON JavaScript Object Notation MVC Model-View-Controller MathML Mathematical Markup Language OS Operating System OpenGL R ES Open Graphics Library for Embedded Systems REST Representational State Transfer RTMP Real Time Messaging Protocol SDK Software Development Kit SQL Structured Query Language SVG Scalable Vector Graphics v TCP Transmission Control Protocol TTL Time To Live UA User-Agent URL Uniform Resource Locator W3C World Wide Web Consortium WHATWG Web Hypertext Application Technology Working Group WWW World Wide Web WebGL Web Graphics Library WiFi Wireless Local Area Network (WLAN) XHTML eXtensible HyperText Markup Language XML Extensible Markup Language vi Contents Abbreviations and Acronyms iv 1 Introduction 1 1.1 Research Questions . 4 1.2 Structure of This Work . 4 2 HTML5 5 2.1 Semantic Markup . 6 2.2 Extensibility . 7 2.3 Media . 8 2.4 Canvas 2D Context . 8 2.5 Form Enhancements . 8 2.6 Session History Manipulation . 9 2.7 Offline Web Applications . 9 2.7.1 Application Cache . 9 2.7.2 Data Storage . 10 2.7.3 Detecting Network State . 11 2.8 Drag and Drop . 11 2.9 SVG and MathML . 11 3 Other Related Specifications 13 3.1 Cascading Style Sheets . 13 3.2 WebGL and Typed Arrays . 15 3.3 Touch Events . 16 3.4 Files . 16 3.5 Web Real-time Communication . 16 3.6 Web Sockets . 17 3.7 Server-Sent Events . 18 3.8 Web Workers . 18 3.9 Analytics and Timing . 19 3.10 Page Visibility and Timer Control . 19 vii 3.11 Cross-Origin Resource Sharing . 20 3.12 Device APIs . 20 3.12.1 Geolocation . 20 3.12.2 Device Orientation . 21 3.12.3 User Media . 21 3.13 Other . 21 4 Tools and Techniques 23 4.1 Single-Page applications . 24 4.2 JavaScript MVC Libraries . 24 4.3 Responsive Design . 26 4.4 Progressive Enhancement . 26 4.5 User Interface Libraries . 27 4.5.1 jQuery Mobile . 27 4.5.2 jQTouch . 27 4.5.3 Sencha Touch . 29 4.6 Hybrid Applications . 29 4.7 Performance Guidelines . 30 5 Use Case 36 5.1 Conference Application Requirements . 36 5.2 JSONCache Requirements . 37 6 Implementation and Results 38 6.1 Conference Application . 38 6.2 JSONCache JavaScript Library . 40 6.3 Targeting Different Platforms . 42 6.3.1 Device Detection . 43 6.3.2 Feature Detection . 44 6.4 Targeting Different Screens . 46 6.5 Handling Different Orientations . 48 6.6 Handling Mobile Networks . 49 6.6.1 Minimizing Data Transfer . 49 6.6.2 Caching . 49 6.6.3 Preloading . 50 6.6.4 Offline Support . 50 6.6.5 Handling Interruptions . 51 6.7 Animations . 52 6.8 Following JavaScript Best Practices . 52 6.8.1 JSLint . 52 6.8.2 Lazy initialization . 53 viii 6.8.3 Efficient DOM Manipulation . 53 6.8.4 Efficient Event Handling . 53 6.9 Performance Analysis . 54 6.9.1 YSlow . 54 6.9.2 Page Speed . 55 6.10 Summary of Results . 59 7 Conclusions 62 7.1 Further Work . 62 7.2 Discussion . 63 ix Chapter 1 Introduction Twenty years after its birth, the Web has become one of the defining tech- nological innovations that knows no geographical, political, or ideological boundaries. The world wide platform built on top of the physical Internet is deeply integrated into our daily lives. This powerful tool that was built on egalitarian principles is now taken for granted, just like old innovations such as electricity. [4] In parallel with the rapid growth of the Web, mobile phones have evolved from briefcase-sized \portable" telephony devices into modern pocket-sized computers. The mobile revolution has already changed the world as we see it, and more people have access to the Web from a mobile device than from an Internet-connected desktop computer. [15] The Web is not constrained into (desktop and laptop) computers and mobile phones, though. Tablets, TVs, ebook readers, watches, and even household appliances are connecting to the Internet and have web browsers. For the first time in history, we have a truly ubiquitous digital medium. [15] Universal accessibility and openness are the keys to being the ubiquitous information platform of the digital age [4]. Now the Web is closer in accom- plishing its original principles in equality and universality; anyone can access this vast source of open information from anywhere, with any device. All you need is a web browser that supports the open standards of the Web. The goal of the Web is to serve humanity. { Tim Berners-Lee [4] Being the universal digital medium, mobile devices has some unique char- acteristics that other mass media lack. Mobile is personal, always-on, always- carried medium with a built-in payment channel. Mobile is in your pocket at the moment you have your creative impulse.