Editing HTML 5

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 172: Line 172:
  
 
Currently, there are three supported file formats for the <audio> element: MP3, WAV, and OGG.
 
Currently, there are three supported file formats for the <audio> element: MP3, WAV, and OGG.
 
==The Video Element==
 
The video element is similar to the audio element.
 
You can specify the video source URL using an attribute in a video element, or using source elements inside the video element:
 
 
<pre><video controls>
 
  <source src="video.mp4" type="video/mp4">
 
  <source src="video.ogg" type="video/ogg">
 
  Video is not supported by your browser
 
</video></pre>
 
Another aspect that the audio and video elements have in common is that the major browsers do not all support the same file types. If the browser does not support the first video type, it will try the next one.
 
 
===Attributes of <video>===
 
Another aspect shared by both the audio and the video elements is that each has controls, autoplay and loop attributes.
 
 
<pre><video controls autoplay loop>
 
  <source src="video.mp4" type="video/mp4">
 
  <source src="video.ogg" type="video/ogg">
 
  Video is not supported by your browser
 
</video></pre>
 
Currently, there are three supported video formats for the <video> element: MP4, WebM, and OGG
 
 
==Progress Element==
 
The '''<progress>''' element provides the ability to create progress bars on the web.
 
The progress element can be used within headings, paragraphs, or anywhere else in the body.
 
 
<u>Progress Element Attributes</u>
 
'''Value''': Specifies how much of the task has been completed.
 
'''Max''': Specifies how much work the task requires in total.
 
 
Example:
 
 
<pre>Status: <progress min="0" max="100" value="35">
 
</progress></pre>
 
 
==Web Storage API==
 
===HTML5 Web Storage===
 
With HTML5 web storage, websites can store data on a user's local computer.
 
Before HTML5, we had to use '''JavaScript cookies''' to achieve this functionality.
 
 
<u>The Advantages of Web Storage</u>
 
*- More secure
 
*- Faster
 
*- Stores a larger amount of data
 
*- Stored data is not sent with every server request
 
 
Local storage is per domain. All pages from one domain can store and access the same data.
 
 
===Types of Web Storage Objects===
 
There are two types of web storage objects:
 
*- '''sessionStorage()''': Session Storage is destroyed once the user closes the browser
 
*- '''localStorage()''': Local Storage stores data with no expiration date
 
 
===Working with Values===
 
The syntax for web storage for both local and session storage is very simple and similar.
 
The data is stored as key/value pairs.
 
 
'''Storing a Value:'''
 
<pre>localStorage.setItem("key1", "value1");</pre>
 
 
'''Getting a Value:'''
 
<pre>//this will print the value
 
alert(localStorage.getItem("key1")); </pre>
 
 
'''Removing a Value:'''
 
<pre>localStorage.removeItem("key1");</pre>
 
 
'''Removing All Values:'''
 
<pre>localStorage.clear();</pre>
 
 
The same syntax applies to the session storage, with one difference: Instead of localStorage, sessionStorage is used.
 
 
==Geolocation API==
 
In HTML5, the Geolocation API is used to obtain the user's geographical location.
 
Since this can compromise user privacy, the option is not available unless the user approves it.
 
 
===Using HTML Geolocation===
 
The Geolocation API’s main method is getCurrentPosition, which retrieves the current geographic location of the user's device.
 
  navigator.geolocation.getCurrentPosition();
 
 
Parameters:
 
 
*'''showLocation (mandatory)''': Defines the callback method that retrieves location information.
 
*'''ErrorHandler(optional)''': Defines the callback method that is invoked when an error occurs in processing the asynchronous call.
 
*'''Options (optional)''': Defines a set of options for retrieving the location information.
 
 
===Presenting Data===
 
User location can be presented in two ways: '''Geodetic''' and '''Civic'''.
 
 
*1. The '''geodetic''' way to describe position refers directly to '''latitude''' and '''longitude.'''
 
*2. The '''civic''' representation of location data is presented in a format that is more easily read and understood(usual address).
 
 
The getCurrentPosition() method returns an object if it is successful. The latitude, longitude, and accuracy properties are always returned.
 
 
==Drag&Drop API==
 
===Making Elements Draggable===
 
The drag and drop feature lets you "grab" an object and drag it to a different location.
 
To make an element draggable, just set the draggable attribute to true: <code><img draggable="true" /></code>
 
 
Example:
 
 
<pre>
 
<!DOCTYPE HTML>
 
<html>
 
  <head>
 
  <script>
 
function allowDrop(ev) {
 
    ev.preventDefault();
 
}
 
 
function drag(ev) {
 
    ev.dataTransfer.setData("text", ev.target.id);
 
}
 
 
function drop(ev) {
 
    ev.preventDefault();
 
    var data = ev.dataTransfer.getData("text");
 
    ev.target.appendChild(document.getElementById(data));
 
}
 
  </script>
 
  </head>
 
<body>
 
 
  <div id="box" ondrop="drop(event)"
 
  ondragover="allowDrop(event)"
 
  style="border:1px solid black;
 
  width:200px; height:200px"></div>
 
 
  <img id="image" src="sample.jpg" draggable="true"
 
  ondragstart="drag(event)" width="150" height="50" alt="" />
 
 
</body>
 
</html></pre>
 
 
'''What to Drag'''
 
 
When the element is dragged, the ondragstart attribute calls a function, '''drag(event)''', which specifies what data is to be dragged.
 
The '''dataTransfer.setData()''' method sets the data type and the value of the dragged data:
 
 
<pre>function drag(ev) {
 
    ev.dataTransfer.setData("text", ev.target.id);
 
}
 
</pre>
 
 
In our example, the data type is "text" and the value is the ID of the draggable element ("image").
 
 
'''Where to Drop'''
 
 
The '''ondragover''' event specifies where the dragged data can be dropped. By default, data and elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element.
 
This is done by calling the '''event.preventDefault()''' method for the '''ondragover''' event.
 
 
''Do the Drop'''
 
 
When the dragged data is dropped, a drop event occurs.
 
In the example above, the ondrop attribute calls a function, '''drop(event)''':
 
 
<pre>function drop(ev) {
 
    ev.preventDefault();
 
    var data = ev.dataTransfer.getData("text");
 
    ev.target.appendChild(document.getElementById(data));
 
}
 
</pre>
 
 
The '''preventDefault()''' method prevents the browser's default handling of the data (default is open as a link on the drop).
 
The dragged data can be accessed with the '''dataTransfer.getData()''' method. This method will return any data that was set to the same type in the setData() method.
 
The dragged data is the ID of the dragged element ("image").
 
 
In the end, the dragged element is appended into the drop element, using the '''appendChild()''' function.
 
 
Basic knowledge of JavaScript is required to understand and use the API.
 
 
==See also==
 
*[[HTML]]
 

Please note that all contributions to wikieduonline may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Wikieduonline:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)

Advertising: