WT 9
WT 9
HTML5
Introduction
• We can use the method of previous example, for all new HTML5
elements, but Internet Explorer 8 and earlier, does not allow styling of
unknown elements.
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
The code above is a comment, but versions previous to IE9 will read it (and
understand it).
The link to the shiv code must be placed in the <head> element, because
Internet Explorer needs to know about all new elements before reading them.
EXAMPLE
• <article>
• <aside>
• <details>
• <figcaption>
• <figure>
• <header>
• <footer>
• <main>
• <mark>
• <nav>
• <section>
• <summary>
• <time>
EXAMPLE
• The <aside> element defines some content aside from the content it is
placed in (like a sidebar).
• The aside content should be related to the surrounding content.
• The <aside> content could be placed as a sidebar in an article.
Default CSS Settings
• Most browsers will display the <aside> element with the following
default values:
aside{
display: block;
}
EXAMPLE
• The <details> tag specifies additional details that the user can view or
hide on demand.
• The <details> tag can be used to create an interactive widget that the
user can open and close. Any sort of content can be put inside the
<details> tag.
• The content of a <details> element should not be visible unless the open
attribute is set.
• The <summary> tag is used to specify a visible heading for the details.
The heading can be clicked to view/hide the details.
EXAMPLE
• The <summary> tag defines a visible heading for the <details> element.
The heading can be clicked to view/hide the details.
• Default CSS Settings
Most browsers will display the <summary> element with the following
default values:
summary {
display: block;
}
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 30
HTML5 <footer> Element
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
• The <address> tag defines the contact information for the author/owner of a
document or an article.
• If the <address> element is inside the <body> element, it represents contact
information for the document.
• If the <address> element is inside an <article> element, it represents contact
information for that article.
• The text in the <address> element usually renders in italic. Most browsers will add a
line break before and after the address element.
• The <address> tag should NOT be used to describe a postal address, unless it is a
part of the contact information.
• The <address> element will typically be included along with other information in a
<footer> element.
Default CSS Settings
• Most browsers will display the <address> element with the following default values:
address {
display: block;
font-style: italic;
}
EXAMPLE
• The <menuitem> tag defines a command/menu item that the user can
invoke from a popup menu.
• <menu type="popup">
<menuitem type="radio" radiogroup="alignment" checked
label="Left" onclick="setAlign('left')">
</menu>
EXAMPLE
EXAMPLE
EXAMPLE
<menuitem type="command|checkbox|radio"
• The type attribute specifies the type of command/menu item.
• <menu>
<menuitem type="command" label="Save"
onclick="save()">Save</menuitem>
</menu>
<meter high="number">
<meter low="number">
<meter max="number">
<meter min="number">
• The high attribute specifies the range where the gauge's value is considered to be a high value.
• The low attribute specifies the range where the gauge's value is considered to be a low value.
• The max attribute specifies the upper bound of the gauge.
• The min attribute specifies the lower bound of the gauge.
• The high attribute value must be less than the max attribute value, and it also must be greater
than the low and min attribute values.
• The low attribute value must be greater than the min attribute value, and it also must be less than
the high and max attribute values.
• The max attribute value must be greater than the min attribute value. If unspecified, the default
value is 1.
• The min attribute value must be less than the max attribute value. If unspecified, the default
value is 0.
EXAMPLE
• The optimum attribute specifies the range where the gauge's value is
considered to be an optimal value.
<meter optimum="number">
• number, Specifies a floating point number that is the optimal value of
the gauge
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
• In the example, usernames are shown along with the number of points in an
exam.
• If the <bdi> element is not supported in the browser, the username of the
Arabic user would confuse the text (the bidirectional algorithm would put
the colon and the number "6" next to the word "User" rather than next to the
word "points").
EXAMPLE
EXAMPLE
• Bad:
<SECTION>
<p>This is a paragraph.</p>
</SECTION>
• Very Bad:
<Section>
<p>This is a paragraph.</p>
</SECTION>
• Good:
<section>
<p>This is a paragraph.</p>
</section>
• Image Attributes
• Always use the alt attribute with images. It is important when the image
cannot be viewed.
<img src="html5.gif" alt="HTML5" style="width:128px;height:128px">
• Always define image size.
• It reduces flickering because the browser can reserve space for images
before they are loaded.
<img src="html5.gif" alt="HTML5" style="width:128px;height:128px">
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
• Omitting <head>?
• In the HTML5 standard, the <head> tag can also be omitted.
• By default, browsers will add all elements before <body>, to a default
<head> element.
<!DOCTYPE html>
<html>
<title>Page Title</title>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
HTML Comments
• Short comments should be written on one line, with a space after <!--
and a space before -->:
<!-- This is a comment -->
• Long comments, spanning many lines, should be written with <!-- and
--> on separate lines:
<!--
This is a long comment example. This is a long comment example.
This is a long comment example.
This is a long comment example. This is a long comment example.
This is a long comment example.
-->
• Long comments are easier to observe, if they are indented 2 spaces.
• File Extensions
– HTML files should have a .html extension (not .htm).
• The HTML <canvas> element is used to draw graphics, on the fly, via
scripting (usually JavaScript).
• The <canvas> element is only a container for graphics.
• We need to use a script to actually draw the graphics.
• Canvas has several methods for drawing paths, boxes, circles, text, and
adding images.
• A canvas is a rectangular area on an HTML page.
• By default, a canvas has no border and no content.
Syntax
<canvas id="myCanvas" width="200" height="100"></canvas>
• Note: Always specify an id attribute (to be referred to in a script), and a
width and height attribute to define the size of the canvas.
• To add a border, use the style attribute:
EXAMPLE
EXAMPLE
Canvas Coordinates
• The HTML canvas is a two-dimensional grid.
• The upper-left corner of the canvas has the coordinates (0,0)
• In our previous example we used: fillRect(0,0,150,75).
• This means: Start at the upper-left corner (0,0) and draw a 150x75
pixels rectangle.
EXAMPLE
Draw a Line
• To draw a straight line on a canvas, use the following methods:
• moveTo(x,y) - defines the starting point of the line
• lineTo(x,y) - defines the ending point of the line
• To actually draw the line, we must use one of the "ink" methods, like
stroke().
• Define a starting point in position (0,0), and an ending point in position
(200,100). Then use the stroke() method to actually draw the line:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.moveTo(0,0);
ctx.lineTo(200,100);
ctx.stroke();
EXAMPLE
Draw a Circle
• To draw a circle on a canvas, use the following methods:
– beginPath();
– arc(x,y,r,start,stop)
• Define a circle with the arc() method. Then use the stroke() method to
actually draw the circle:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
EXAMPLE
Canvas - Gradients
• Gradients can be used to fill rectangles, circles, lines, text, etc.
• Shapes on the canvas are not limited to solid colors.
• There are two different types of gradients:
– createLinearGradient(x,y,x1,y1) - creates a linear gradient
– createRadialGradient(x,y,r,x1,y1,r1) - creates a radial/circular
gradient
• Once we have a gradient object, we must add two or more color stops.
• The addColorStop() method specifies the color stops, and its position
along the gradient.
• Gradient positions can be anywhere between 0 to 1.
• To use the gradient, set the fillStyle or strokeStyle property to the
gradient, then draw the shape (rectangle, text, or a line).
Example
• Create a linear gradient. Fill rectangle with the gradient:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// Create gradient
var grd=ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
EXAMPLE
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// Create gradient
var grd=ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
EXAMPLE
• Example
• Set font to 30px "Arial" and write a filled text on the canvas:
EXAMPLE
• Example
• Set font to 30px "Arial" and write a text, with no fill, on the canvas:
EXAMPLE
• Example
• Set font to 30px "Comic Sans MS" and write a filled red text in the
center of the canvas:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "30px Comic Sans MS";
ctx.fillStyle = "red";
ctx.textAlign = "center";
ctx.fillText("Hello World", canvas.width/2, canvas.height/2);
EXAMPLE
• Canvas - Images
• To draw an image on a canvas, use the following method:
– drawImage(image,x,y)
window.onload() function () {
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 10, 10);
}
• We cannot draw the image before the image has loaded. Call the
function from window.onload().
EXAMPLE
EXAMPLE
EXAMPLE
• var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.rotate(20*Math.PI/180);
ctx.fillRect(50,20,100,50);
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 87
Canvas Clock
• An analog clock using HTML canvas
Part I - Create the Canvas
• The clock needs an HTML container. Create an 300 x 300 pixel HTML canvas:
<!DOCTYPE html>
<html>
<body>
<canvas id="canvas" width="300" height="300" style="background-color:#333"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var radius = canvas.height / 2;
ctx.translate(radius, radius);
radius = radius * 0.90
drawClock();
function drawClock() {
ctx.arc(0, 0, radius, 0 , 2*Math.PI);
ctx.fillStyle = "white";
ctx.fill();
}
</script>
</body>
</html>
EXAMPLE
• Create a canvas object (var canvas) from the HTML canvas element:
var canvas = document.getElementById("canvas");
• Create a 2d drawing object (var ctx) for the canvas object:
var ctx = canvas.getContext("2d");
• Calculate the clock radius, using the height of the canvas:
var radius = canvas.height / 2;
• Using the canvas height to calculate the clock radius, makes the clock
work for all canvas sizes.
• Remap the (0,0) position (of the drawing object) to the center of the
canvas:
ctx.translate(radius, radius);
• Reduce the clock radius (to 90%) to draw the clock well inside the
canvas:
radius = radius * 0.90;
• Create a function to draw the clock:
function drawClock() {
ctx.arc(0, 0, radius, 0 , 2*Math.PI);
ctx.fillStyle = "white";
ctx.fill();
}
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 95
Example Explained
• Set the font size (of the drawing object) to 15% of the radius:
ctx.font = radius*0.15 + "px arial";
• Set the text alignment to the middle and the center of the print position:
ctx.textBaseline="middle";
ctx.textAlign="center";
• Calculate the print position (for 12 numbers) to 85% of the radius, rotated
(PI/6) for each number:
for(num= 1; num < 13; num++) {
ang = num * Math.PI / 6;
ctx.rotate(ang);
ctx.translate(0, -radius*0.85);
ctx.rotate(-ang);
ctx.fillText(num.toString(), 0, 0);
ctx.rotate(ang);
ctx.translate(0, radius*0.85);
ctx.rotate(-ang);
}
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 97
Example Explained
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 99
Example Explained
• The only thing you have to do (to start the clock) is to call the
drawClock function at intervals.
Substitute:
• drawClock();
With:
• setInterval(drawClock, 1000);
• The interval is in milliseconds. drawClock will be called for each 1000
milliseconds.
• Advantages of using SVG over other image formats (like JPEG and
GIF) are:
• SVG images can be created and edited with any text editor
• SVG images can be searched, indexed, scripted, and compressed
• SVG images are scalable
• SVG images can be printed with high quality at any resolution
• SVG images are zoomable (and the image can be zoomed without
degradation)
• SVG is an open standard
• SVG files are pure XML
• The main competitor to SVG is Flash.
• The biggest advantage SVG has over Flash is the compliance with other
standards (e.g. XSL and the DOM). Flash relies on proprietary
technology that is not open source.
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 104
SVG Shapes
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 106
Other <rect> Example
• <svg width="400" height="180">
<rect x="50" y="20" width="150" height="150“ style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
</svg>
• The x attribute defines the left position of the rectangle (e.g. x="50" places the
rectangle 50 px from the left margin)
• The y attribute defines the top position of the rectangle (e.g. y="20" places the
rectangle 20 px from the top margin)
• The CSS fill-opacity property defines the opacity of the fill color (legal range: 0
to 1)
• The CSS stroke-opacity property defines the opacity of the stroke color (legal
range: 0 to 1)
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 107
Other <rect> Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 108
Other <rect> Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 109
SVG Circle - <circle>
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 110
SVG Ellipse - <ellipse>
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 111
Other <ellipse> example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 112
Other <ellipse> example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 113
SVG Line - <line>
• The
• <svg height="210" width="500">
<line x1="0" y1="0" x2="200" y2="200" style="stroke:rgb(255,0,0);stroke-width:2" />
</svg>
• <line> element is used to create a line:
• The x1 attribute defines the start of the line on the x-axis
• The y1 attribute defines the start of the line on the y-axis
• The x2 attribute defines the end of the line on the x-axis
• The y2 attribute defines the end of the line on the y-axis
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 114
SVG Polygon - <polygon>
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 115
Other <polygon> Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 116
Other <polygon> Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 117
Other <polygon> Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 118
SVG Polyline - <polyline>
• The <polyline> element is used to create any shape that consists of only
straight lines:
<svg height="200" width="500">
<polyline points="20,20 40,25 60,40 80,120 120,140 200,180"
style="fill:none;stroke:black;stroke-width:3" />
</svg>
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 119
Other <polyline> Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 120
SVG Path - <path>
• The example below defines a path that starts at position 150,0 with a
line to position 75,200 then from there, a line to 225,200 and finally
closing the path back to 150,0:
• <svg height="210" width="400">
<path d="M150 0 L75 200 L225 200 Z" />
</svg>
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 122
SVG Text - <text>
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 123
Transform="rotate()” example
<!DOCTYPE html>
<html>
<body>
<rect x="20" y="70" width="40" height="40“ style="fill: #3333cc“ transform="rotate(15, 40, 40)“ />
Sorry, your browser does not support inline SVG.
</svg>
</body>
</html>
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 124
Example <text> <tspan>
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 125
Example <text> Text as a link
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 126
SVG Stroke Properties
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 127
SVG stroke-width Property
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 128
SVG stroke-linecap Property
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 129
SVG stroke-dasharray Property
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 130
SVG Filters
• SVG Filters are used to add special effects to SVG graphics.
• The available filter elements in SVG are:
– <feBlend> - filter for combining images
– <feColorMatrix> - filter for color transforms
– <feComponentTransfer>
– <feComposite>
– <feConvolveMatrix>
– <feDiffuseLighting>
– <feDisplacementMap>
– <feFlood>
– <feGaussianBlur>
– <feImage>
– <feMerge>
– <feMorphology>
– <feOffset> - filter for drop shadows
– <feSpecularLighting>
– <feTile>
– <feTurbulence>
– <feDistantLight> - filter for lighting
– <fePointLight> - filter for lighting
– <feSpotLight> - filter for lighting
• We can use multiple filters on each SVG element!
• Internet Explorer 9 and earlier versions do not support SVG filters.
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 131
<defs> and <filter>
• All internet SVG filters are defined within a <defs> element. The
<defs> element is short for definitions and contains definition of special
elements (such as filters).
• The <filter> element is used to define an SVG filter. The <filter>
element has a required id attribute which identifies the filter. The
graphic then points to the filter to use.
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 133
SVG Drop Shadows
• SVG <feOffset>
• The <feOffset> element is used to create drop shadow effects. The idea is to
take an SVG graphic (image or element) and move it a little bit in the xy
plane.
• The first example offsets a rectangle (with <feOffset>), then blend the
original on top of the offset image (with <feBlend>):
<svg height="120" width="120">
<defs>
<filter id="f1" x="0" y="0" width="200%" height="200%">
<feOffset result="offOut" in="SourceGraphic" dx="20" dy="20" />
<feBlend in="SourceGraphic" in2="offOut" mode="normal" />
</filter>
</defs>
<rect width="90" height="90" stroke="green" stroke-width="3"
fill="yellow" filter="url(#f1)" />
</svg>
• The id attribute of the <filter> element defines a unique name for the filter
• The filter attribute of the <rect> element links the element to the "f1" filter
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 134
Other Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 135
Other Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 137
SVG Gradients
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 140
Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 141
SVG Radial Gradient - <radialGradient>
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 143
Example
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 144
SVG Examples (Basic Shapes)
1. A circle
2. A rectangle
3. A rectangle with opacity
4. A rectangle with opacity 2
5. A rectangle with rounded corners
6. An ellipse
7. Three ellipses on top of each other
8. Two ellipses
9. A line
10. A polygon with three sides
11. A polygon with four sides
12. A star
13. Another star
14. A polyline
15. Another polyline
16. A path
17. A quadratic Bézier curve
18. Write a text
19. Rotate a text
20. Text on several lines
21. Text as a link
22. Defines the color of a line, text or outline (stroke)
23. Defines the width of a line, text or outline (stroke-width)
24. Defines different types of endings to an open path (stroke-linecap)
25. Defines dashed lines (stroke-dasharray)
• The <video> tag specifies video, such as a movie clip or other video
streams.
• Currently, there are 3 supported video formats for the <video> element:
MP4, WebM, and Ogg:
• MP4 = MPEG 4 files with H264 video codec and AAC audio codec
• WebM = WebM files with VP8 video codec and Vorbis audio codec
• Ogg = Ogg files with Theora video codec and Vorbis audio codec
• MIME Types for Video Formats
Format MIME-type
MP4 video/mp4
WebM video/webm
Ogg video/ogg
• Any text between the <video> and </video> tags will be displayed in browsers that do not
support the <video> element.
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 148
Optional Attributes for <video>
autoplay autoplay Specifies that the video will start playing as soon as it is
ready
controls controls Specifies that video controls should be displayed (such
as a play/pause button etc).
height pixels Sets the height of the video player
loop loop Specifies that the video will start over again, every time
it is finished
muted muted Specifies that the audio output of the video should be
muted
poster URL Specifies an image to be shown while the video is
downloading, or until the user hits the play button
preload auto Specifies if and how the author thinks the video should
metadata be loaded when the page loads
none
src URL Specifies the URL of the video file
width pixels Sets the width of the video player
• HTML Geolocation
• HTML Drag/Drop
• HTML Local Storage
• HTML App Cache
• HTML Web Workers
• HTML SSE
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 157
Using HTML Geolocation
• Use the getCurrentPosition() method to get the user's position.
• The example below is a simple Geolocation example returning the latitude and
longitude of the user's position:
<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>
• If Geolocation is 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
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 158
Handling Errors and Rejections
• getCurrentPosition() method is used to handle errors. It specifies a function to run if
it fails to get the user's location:
• function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
• Permission denied - The user did not allow Geolocation
• Position unavailable - It is not possible to get the current location
• Timeout - The operation timed out
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 159
Displaying the Result in a Map
• To display the result in a map, you need access to a map service that can
use latitude and longitude, like Google Maps
• function showPosition(position) {
var latlon = position.coords.latitude + "," + position.coords.longitude;
document.getElementById("mapholder").innerHTML = "<img
src='"+img_url+"'>";
}
• In the example above we use the returned latitude and longitude data to
show the location in a Google map (using a static image).
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 160
The getCurrentPosition() Method - Return Data
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 162
HTML5 Drag and Drop
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 163
• Make an Element Draggable
• First of all: To make an element draggable, set the draggable attribute to
true:
<img draggable="true">
• What to Drag - ondragstart and setData()
• Then, specify what should happen when the element is dragged.
• In the example , the ondragstart attribute calls a function, drag(event),
that specifies what data to be dragged.
• The dataTransfer.setData() method sets the data type and the value of
the dragged data:
• function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
• In this case, the data type is "text" and the value is the id of the
draggable element ("drag1").
•
• With local storage, web applications can store data locally within the
user's browser.
• Before HTML5, application data had to be stored in cookies, included
in every server request. Local storage is more secure, and large amounts
of data can be stored locally, without affecting website performance.
• Unlike cookies(4KB per cookie), the storage limit is far larger (at least
5MB) and information is never transferred to the server.
• Local storage is per domain. All pages, from one domain, can store and
access the same data.
• HTML local storage provides two objects for storing data on the client:
• window.localStorage - stores data with no expiration date
• window.sessionStorage - stores data for one session (data is lost when
the tab is closed)
• Before using local storage, check browser support for localStorage and
sessionStorage:
• if(typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
} else {
// Sorry! No Web Storage support..
}
• The localStorage object stores the data with no expiration date. The data
will not be deleted when the browser is closed, and will be available the
next day, week, or year.
• Example
• // Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML =
localStorage.getItem("lastname");
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 169
The sessionStorage Object
EXAMPLE
Web Technology / Mirzanur Rahman / Dept of IT / GU /2015 170
HTML5 Application Cache
<body>
The content of the document......
</body>
</html>
• The manifest file is a simple text file, which tells the browser what to
cache (and what to never cache).
• The manifest file has three sections:
• CACHE MANIFEST - Files listed under this header will be cached
after they are downloaded for the first time
• NETWORK - Files listed under this header require a connection to the
server, and will never be cached
• FALLBACK - Files listed under this header specifies fallback pages if
a page is inaccessible
EXAMPLE
function timedCount() {
i = i + 1;
postMessage(i);
setTimeout("timedCount()",500);
}
timedCount();
• The important part of the code above is the postMessage() method - which
is used to post a message back to the HTML page.
• Note: Normally web workers are not used for such simple scripts, but for
more CPU intensive tasks.
• Example explained:
• Create a new EventSource object, and specify the URL of the page sending
the updates (in this example "demo_sse.php")
• Each time an update is received, the onmessage event occurs
• When an onmessage event occurs, put the received data into the element
with id="result"
$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>
Events Description
When a connection to the
onopen
server is opened
onmessage When a message is received
onerror When an error occurs