Detection Techniques
Detection Techniques
There are four basic techniques for detecting whether a browser supports a particular feature. From simplest to most complex: #1:Check if a certain property exists on a global object (such as window or navigator).For an example of testing for geolocation support. #2:Create an element, then check if a certain property exists on that element.For an example of testing for canvas support. #3:Create an element, check if a certain method exists on that element, then call themethod and check the value it returns.For an example of testing which video formats are supported. #4:Create an element, set a property to a certain value, then check if the property hasretained its value.For an example of testing which <input> types are supported.
Modernizr is an open-source JavaScript library that helps you build the next generation
of HTML5 and CSS3-powered websites.
Canvas
HTML5 defines the<canvas> element as a resolution-dependent bitmap canvas which can be used for rendering graphs, game graphics, or other visual images on the fly..and defines a set of functions (the canvas API) for drawing shapes, defining paths, creating gradients, and applying transformations You can check for canvas support using this function: function supports_canvas() { return !!document.createElement(canvas).getContext;
} or we can use Modernizr for check if your browser support canvas. if (Modernizr.canvas) { // lets draw some shapes! } else { // no native canvas support available }
Video
HTML5 defines a new element called for embedding video in your web pages.Checking for video support uses detection technique #2.if your browser dosent support video there is a solution called Video for Everybody! This solution uses no JavaScript whatsoever, and it works in virtually every browser, including mobile browsers. You can check for video support using this function: function supports_video() { return !!document.createElement(video).canPlayType; } or we can use Modernizr for check if your browser support Video.
Video Formats
Video formats are like languages.The language of a video is called a codec.Checking for video format support uses detection technique #3. function supports_h264_baseline_video() { if (!supports_video()) { return false; } var v = document.createElement(video); return v.canPlayType(video/mp4,ogg or webm; codecs=avc1.42E01E, mp4a.40.2,theora, vorbisorvp8, vorbis); } The canPlayType() function doesnt return true or false. In recognition of how complex video formats are, the function returns a string:
probably
If the browser is certain it cant play this format. or we can use Modernizr for check if your browser support Video Formats
Local Storage
HTML5 Storageprovides a way for websites to store information on your computer and retrieve it later. Checking for HTML5 Storage support uses detection technique #1.You can check for local storage support using this function: function supports_local_storage() { return (localStorage in window) && window['localStorage'] !== null; } Instead of writing this function yourself, you can use Modernizr.
Web Workers
Web workers provide a standard way for browsers to run JavaScript in the background. With web workers, you can spawn multiple threads that all run at the same time,more or less.Checking for web workers uses detection technique #1.This function checks for web worker support: function supports_web_workers() { return !!window.Worker; } Instead of writing this function yourself, you can use Modernizr.
Geolocation
Geolocation is the art of figuring out where you are in the world and (optionally) sharing that information with people you trust. Checking for geolocation support uses detection technique #1 Heres how to checkfor geolocation support: function supports_geolocation() { return !!navigator.geolocation; } Instead of writing this function yourself, you can use Modernizr
Input Types
Checking for HTML5 input types uses detection technique #4. function supports_Input_Type() { if (!supports_Input_Type()) { return false; } var i = document.createElement(input); i.setAttribute(type, color); return i.type !== color; } Instead of writing this function yourself, you can use Modernizr.
Placeholder Text
Placeholder text is displayed inside the input field as long as the field is empty and not focused. As soon as you click on (or tab to) the input field, the placeholder text disappears
. Checking for placeholder support uses detection technique #2.Heres how to check for placeholder support: function supports_input_placeholder() { var i = document.createElement(input); return placeholder in i; } Instead of writing this function yourself, you can use Modernizr.
Form Autofocus
use to focus the first input field of a web form automatically.
Checking for autofocus support uses detection technique #2. You can detect autofocus support with this function: function supports_input_autofocus() { var i = document.createElement(input);
return autofocus in i; } Instead of writing this function yourself, you can use Modernizr.
Q5.
HTML5 has several new input types for forms. These new features allow better input control and validation. This chapter covers the new input types: color date datetime datetime-local email month number range search tel time url week
Note: Not all major browsers support all the new input types. However, you can already start using them; If they are not supported, they will behave as regular text fields.
Example
Select a color from a color picker:
Try it yourself
Example
Define a date control:
Try it yourself
Example
Define a date and time control (with time zone):
Try it yourself
Example
Define a date and time control (no time zone):
Try it yourself
Example
Define a field for an e-mail address (will be automatically validated when submitted):
Try it yourself Tip: Safari on iPhone recognizes the email type, and changes the on-screen keyboard to match it (adds @ and .com options).
Example
Define a month and year control (no time zone):
Try it yourself
Example
Define a numeric field (with restrictions):
Try it yourself Use the following attributes to specify restrictions: max - specifies the maximum value allowed min - specifies the minimum value allowed step - specifies the legal number intervals value - Specifies the default value
Example
Define a control for entering a number whose exact value is not important (like a slider control):
Try it yourself Use the following attributes to specify restrictions: max - specifies the maximum value allowed min - specifies the minimum value allowed step - specifies the legal number intervals value - Specifies the default value
Example
Define a search field (like a site search, or Google search):
Try it yourself
Try it yourself
Example
Define a control for entering a time (no time zone):
Try it yourself
Example
Define a field for entering a URL:
Try it yourself Tip: Safari on iPhone recognizes the url input type, and changes the on-screen keyboard to match it (adds .com option).
Example
Define a week and year control (no time zone):
Try it yourself
1 2 3
site.
<video src="http://v2v.cc/~j/theora_testsuite/320x240.ogg" controls> Your browser does not support the <code>video</code> element. </video>
This example plays a sample video, with playback controls, from the Theora web
1 <audio src="/test/audio.ogg"> <p>Your browser does not support the audio element.</p> 2 </audio> 3 The src attribute can be a URL of the audio file or the path to the file on the local
system.
1 <audio src="audio.ogg" controls autoplay loop> <p>Your browser does not support the audio element </p> 2 </audio> 3 This code example uses attributes of the <audio> element:
controls : Displays the standard HTML5 controls for the audio on the web page. autoplay : Makes the audio play automatically.
"none" does not buffer the file "auto" buffers the media file "metadata" buffers only the metadata for the file Multiple source files can be specified using the <source> element in order to provide video or audio encoded in different formats for different browsers. For instance:
1 2 3 4 5
<video controls> <source src="foo.ogg" type="video/ogg"> <source src="foo.mp4" type="video/mp4"> Your browser does not support the <code>video</code> element. </video>
This plays the Ogg file in browsers supporting the Ogg format. If the browser doesn't support Ogg, the browser uses the MPEG-4 file. See also the list of media formats supported by the audio and video elements in different browsers. You may also specify which codecs the media file requires; this allows the browser to make even more intelligent decisions:
1 2 3 4
<video controls> <source src="foo.ogg" type="video/ogg; codecs=dirac, speex"> Your browser does not support the <code>video</code> element. </video>
Before using web storage, check browser support for localStorage and sessionStorage:
if(typeof(Storage)!=="undefined") { // Yes! localStorage and sessionStorage support! // Some code..... } else { // Sorry! No web storage support.. }
Example
localStorage.lastname="Smith"; document.getElementById("result").innerHTML="Last name: " + localStorage.lastname;
Try it yourself Example explained: Create a localStorage key/value pair with key="lastname" and value="Smith" Retrieve the value of the "lastname" key and insert it into the element with id="result"
Tip: Key/value pairs are always stored as strings. Remember to convert them to another format when needed. The following example counts the number of times a user has clicked a button. In this code the value string is converted to a number to be able to increase the counter:
Example
if (localStorage.clickcount) { localStorage.clickcount=Number(localStorage.clickcount)+1; }
else { localStorage.clickcount=1; } document.getElementById("result").innerHTML="You have clicked the button " + localStorage.clickcount + " time(s).";
Try it yourself
Example
if (sessionStorage.clickcount) { sessionStorage.clickcount=Number(sessionStorage.clickcount)+1; } else { sessionStorage.clickcount=1; } document.getElementById("result").innerHTML="You have clicked the button " + sessionStorage.clickcount + " time(s) in this session.";
Try it yourself
What is Canvas?
The HTML5 <canvas> element is used to draw graphics, on the fly, via scripting (usually JavaScript). The <canvas> element is only a container for graphics. You must use a script to actually draw the graphics.
Canvas has several methods for drawing paths, boxes, circles, characters, and adding images.
Browser Support
Internet Explorer 9+, Firefox, Opera, Chrome, and Safari support the <canvas> element. Note: Internet Explorer 8 and earlier versions, do not support the <canvas> element.
Create a Canvas
A canvas is a rectangular area on an HTML page, and it is specified with the <canvas> element. Note: By default, the <canvas> element has no border and no content. The markup looks like this:
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"> Your browser does not support the HTML5 canvas tag. </canvas>
</body> </html>
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;"> Your browser does not support the HTML5 canvas tag.</canvas>
<script>
</script>
</body> </html>
MIME TYPES
This book is about HTML5, not previous versions of HTML, and not any version of XHTML. But to understand the history of HTML5 and the motivations behind it, you need to understand a few technical details first. Specifically, MIME types.
Every time your web browser requests a page, the web server sends headers before it sends the actual page markup. These headers are normally invisible, although there are web development tools that will make them visible if youre interested. But the headers are important, because they tell your browser how to interpret the page markup that follows. The most important header is called Content-Type, and it looks like this: Content-Type: text/html
text/html is called the content type or MIME type of the page. This header is the only thing that determines what a particular resource truly is, and therefore how it should be rendered. Images have their own MIME types (image/jpeg forJPEG images, image/png for PNG images, and so on). JavaScript files have their own MIME type. CSS stylesheets have their own MIME type. Everything has its own MIME type. The web runs on MIME types.
Of course, reality is more complicated than that. The first generation of web servers (and Im talking web servers from 1993) didnt send the Content-Type header because it didnt exist yet. (It wasnt invented until 1994.) For compatibility reasons that date all the way back to 1993, some popular web browsers will ignore the Content-Type header under certain circumstances. (This is called content sniffing.) But as a general rule of thumb, everything youve ever looked
at on the web HTMLpages, images, scripts, videos, PDFs, anything with a URL has been served to you with a specific MIME type in theContent-Type header.
Browser Support
Internet Explorer 9+, Firefox, Chrome, Safari and Opera support Geolocation. Note: Geolocation is much more accurate for devices with GPS, like iPhone.
Example
<script> var x=document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); }
else{x.innerHTML="Geolocation is not supported by this browser.";} } function showPosition(position) { x.innerHTML="Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; } </script>
Example explained: Check if Geolocation is supported If supported, run the getCurrentPosition() method. If not, display a message to the user If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter ( showPosition ) The showPosition() function gets the displays the Latitude and Longitude
The example above is a very basic Geolocation script, with no error handling. Errorhandling
HTML4 supports what is called tag soup, this is, the ability to write malformed code and have it corrected into a valid document. But the problem is that rules for doing this arent written anywhere, so developers just have to test malformed documents in various browsers (especially Internet Explorer) to handle any errors. Also, HTML4 lacks rules for parsing, which makes it more difficult to handle errors. HTML5 is attempting to solve this, so that browser developers can standardize and save time and money. It is also more flexible to handle inaccurate syntax, and it specifies the rules related to the parsing and lexing.
What is W3C?
W3C W3C W3C W3C W3C W3C W3C W3C Stands for the World Wide Web Consortium was created in October 1994 was created by Tim Berners-Lee was created by the Inventor of the Web is organized as a Member Organization is working to Standardize the Web creates and maintains WWW Standards Standards are called W3C Recommendations
How it Started
The World Wide Web (WWW) began as a project at the European Organization for Nuclear Research (CERN), where Tim Berners-Lee developed a vision of the World Wide Web. Tim Berners-Lee - the inventor of the World Wide Web - is now the Director of the World Wide Web Consortium (W3C). W3C was created in 1994 as a collaboration between the Massachusetts Institute of Technology (MIT) and the European Organization for Nuclear Research (CERN), with support from the U.S. Defense Advanced Research Project Agency (DARPA) and the European Commission.
W3C Members
Because the Web is so important (both in scope and in investment) that no single organization should have control over its future, W3C functions as a member organization. Some well known members are: IBM Microsoft America Online Apple Adobe Macromedia
Sun Microsystems
The Full List of Member Organizations includes a variety of software vendors, content providers, corporate users, telecommunications companies, academic institutions, research laboratories, standards bodies, and governments.
W3C Recommendations
The most important work done by the W3C is the development of Web specifications (called "Recommendations") that describe communication protocols (like HTML and XML) and other building blocks of the Web. Each W3C Recommendation is developed by a work group consisting of members and invited experts. The group obtains its input from companies and other organizations, and creates a Working Draft and finally a Proposed Recommendation. In general the Recommendation is submitted to the W3C membership and director, for a formal approval as a W3C Recommendation.
HTML5 is a Revolution
By Max On November 25, 2012 Leave a Comment In Codiqa, html5
HTML5 is a semi-buzzword that is thrown around quite a bit these days. What exactly does it mean, and why does it matter? Technically, the term has a simple definition: HTML5 is the 5th revision of the HTML standard that Web Browsers use to display web pages. This provides a convenient way for browser developers like Mozilla, Google, Microsoft, and Opera to produce browsers that generally behave in a similar, standardized fashion (though the reality is less perfect). The 5th revision of the HTML standard, or HTML5, introduces many new features that make browsers more powerful and capable of providing modern web experiences. But what makes HTML5 truly interesting is not just the list of technological advancements. HTML5 is a revolution for the open web. HTML5: the empowerment of open web technologies.
With HTML5, our web browsers can do increasingly amazing things that were once the domain of locked-down proprietary platforms like iOS and Adobe Flash. We can access the camera on a mobile device, get the users current location through GPS, display video and play audio, or render complex 3D scenes. That, all with open web technologies that we can all use, generally for free, and with very open licenses. HTML5 is all about giving our open platforms more power to deliver increasingly amazing and complex products and experiences, in a way that is accessible to the largest number of people, and not controlled by corporate interests. Reducing Barriers to Entry Previously, developing for the web or mobile had to be done through proprietary systems, often for a decent cost. For example, if you want to build traditional iOS apps, you need a Mac (say, for $1500) and a $99/yr developer license just to build and release your app. If you were building Flash applications to provide animated content or stream video to a website, you had to purchase Adobe Flash Professional for $699. Today, you can build amazing mobile and desktop applications with HTML5 and open web technologies. All you need is a text editor and a browser, both of which are free. HTML5 is the Future As it stands, more companies and developers stand to gain through the greater adoption of HTML5 than with proprietary platforms like iOS. As such, investment and development is increasing for HTML5 tools, services, and products. Its this movement that excited us enough to build Codiqa and join the growing ecosystem of HTML5-based development tools. We want to help move more mobile development over to HTML5 technologies and frameworks, such as jQuery Mobile. Looking into the future, we strongly believe that HTML5 and open web technologies will increasingly become the standard for mobile and desktop development. We dont recommend betting against the open web.
Getting Started To get started with HTML5 as fast as possible, we recommend Dave Woods short HTML5 Tutorial. Once youve got the basics down, one of the best resources for really digging into HTML5 is Dive Into HTML5 by Mark Pilgrim. To get started with HTML5 development for mobile, we recommend the great HTML5 mobile framework jQuery Mobile which Codiqa is based on (our tool helps you visually build mobile apps with jQuery Mobile). Are you new to HTML5, a novice, or an expert? What exciting things has HTML5 enabled you to do, or what are you interested in doing?
<details> <dialog> <summary> <figure> <figcaption> <footer> <header> <hgroup> <mark> <meter> <nav> <progress> <ruby> <rt> <rp>
Defines additional details that the user can view or hide Defines a dialog box or window Defines a visible heading for a <details> element Specifies self-contained content, like illustrations, diagrams, photos, code listings, etc. Defines a caption for a <figure> element Defines a footer for a document or section Defines a header for a document or section Groups a set of <h1> to <h6> elements when a heading has multiple levels Defines marked/highlighted text Defines a scalar measurement within a known range (a gauge) Defines navigation links Represents the progress of a task Defines a ruby annotation (for East Asian typography) Defines an explanation/pronunciation of characters (for East Asian typography) Defines what to show in browsers that do not support ruby annotations
HTML5 Audio
HTML5 provides a standard for playing audio files.
Today, most audio files are played through a plug-in (like flash). However, different browsers may have different plug-ins. HTML5 defines a new element which specifies a standard way to embed an audio file on a web page: the <audio> element.
Browser Support
Internet Explorer 9+, Firefox, Opera, Chrome, and Safari support the <audio> element. Note: Internet Explorer 8 and earlier versions, do not support the <audio> element.
<audio controls> <source src="horse.ogg" type="audio/ogg"> <source src="horse.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>
The control attribute adds audio controls, like play, pause, and volume. You should also insert text content between the <audio> and </audio> tags for browsers that do not support the <audio> element. The <audio> element allows multiple <source> elements. <source> elements can link to different audio files. The browser will use the first recognized format.
Example
How to insert an image:
Browser Support
Note: Not all major browsers support all the new form elements. However, you can already start using them; If they are not supported, they will behave as regular text fields.
Example
An <input> element with pre-defined values in a <datalist>:
<input list="browsers"> <datalist id="browsers"> <option value="Internet Explorer"> <option value="Firefox"> <option value="Chrome"> <option value="Opera"> <option value="Safari"> </datalist>
Example
A form with a keygen field:
<form action="demo_keygen.asp" method="get"> Username: <input type="text" name="usr_name"> Encryption: <keygen name="security"> <input type="submit"> </form>
Example
Perform a calculation and show the result in an <output> element:
<form oninput="x.value=parseInt(a.value)+parseInt(b.value)">0 <input type="range" id="a" value="50">100 + <input type="number" id="b" value="50">= <output name="x" for="a b"></output> </form>