JQuery, JavaScript, and HTML5 - Scott Sanderson
JQuery, JavaScript, and HTML5 - Scott Sanderson
By
Scott Sanderson
TABLE OF CONTENTS
Chapter 1: Introduction
Chapter 2: Basics of HTML5
Chapter 3: Basics of JavaScript
Chapter 4: Basics of CSS3
Chapter 5: HTML5 Explained
Chapter 6: JavaScript and jQuery
Chapter 7: Forms
Chapter 8: Web Services
Chapter 9: WebSocket Communications
Chapter 10: Managing Local Data With the Help of Web Storage
Chapter 11: Offline Web Applications
Appendix
Copyright 2015 by Globalized Healing, LLC - All rights reserved.
CHAPTER 1: INTRODUCTION
If there is one application development framework that can be termed as comprehensive,
then HTML5 wins the bid hands down. Although, the specification for HTML5 is not yet
complete, most modern web browsers support the popular features on device, which range
from a Smartphone to a desktop. What that means is that you just have to write one
application and it will run on your devices without any interoperability issues.
There is no doubt about the fact that HTML5 is the future of web application development
and if you wish to remain in the league, you need to think futuristically and equip yourself
to deal the technological challenges that the future is about to throw at you. The scope of
HTML5 is evident from the fact that most of the major players of the industry and setting
their eyes on this technology and giving in full support to the same.
If the multi-faceted functionality and high-on features characteristics of HTML5 intrigue
you and you want to start writing your own applications right away, but you do not know
how and where to begin, then this book is just for you. This book covers everything that
you shall require to create working applications with the help of HTML,
JavaScript/JQuery and CSS. However, it is not a reference guide. We hope to give you
practical knowledge so that you can get to development as quickly as possible.
This book is a perfect start-up guide and covers all the basic facets of HTML5, JavaScript
and CSS development. It covers everything from the very basics to all that you shall
require in your tryst with this framework. The first three chapters introduce you to these
three technologies, giving you some ground to start with.
defined structure.
The W3C (World Wide Web Consortium) introduced XHTML as an attempt to fix the
flaws of HTML. According to the XHTML specification, HTML documents were forced
to adhere to the format specifications used for XML. Therefore, this allowed the use of
XSD tools for validation of HTML documents. Although, the integration of XML in the
framework fixed some issues, some issues continued to crop up. One of the staggering
issues of the modern times was the growing need for integration of multimedia. While
CSS did perform formatting of some level, it was becoming inadequate for the growing
demands of users.
In order to provide support for interactivity and animated visuals, a programmable support
called JavaScript was added to this ensemble. However, initial versions of this support
were difficult for programmers to understand and slow on execution time incurred. This
led to the introduction of plug-ins like Flash to get the attention that it did. These plugins
did what was expected of them, but the browser-multimedia integration was still loose in
nature.
HTML5 is not an evolved form of XHTML. On the contrary, HTML5 can be better
described as the reinvented form of HTML 4.01 and how HTML, CSS and JavaScript can
be used together to solve the growing needs of the user base.
Semantic Markup
The fundamental feature of HTML5 is that it stresses on separation of behaviour,
presentation and structure. The semantic markup of a website development specifies the
structure of the document. In other words, it specifies the meaning of tags and what they
will do for you. On the other hand, behaviour and presentation are governed by CSS and
JavaScript respectively.
HTML5 Elements
In HTML, an element is simply a statement that contains a beginning tag, content and a
closing tag. Therefore, when you write,
<div>
<b>This is my world!</b>
</div>
In this example, the div elements includes everything from <div> to </div>. therefore, the
<b></b> tag is also a part of the div element.
It is important to state here that HTML5 is not case sensitive. Therefore, regardless of
whether you write <B> or <b> for the bold tag, the browser will consider the two same.
However, the use of lower case for tags is recommended by the W3C standards.
Working with Elements in HTML5
HTML5 defines more than a 100 elements. These elements, with their definitions are
provided in Appendix.
How to add attributes to elements?
Additional data can be added to the begin tag in the form of attributes. An attribute can be
generally represented as, name=value. The value for the attribute name is enclosed
within quotes. There is no restriction on the number of attributes that can be added to the
begin tag. For these attributes, the attribute has a name, but it does not have a value.
Example:
<div id=main class=mainContent></div>
Here, id and class are attributes where id uniquely identifies the element and class
specifies the CSS style to which this div belongs.
Boolean Attributes
Several types of attributes can be used. One of the most commonly used types is Boolean
attribute. The Boolean attribute can take a value from the allowable set of values. Some
possible values include:
Checked
Disabled
Selected
Readonly
There are two ways to indicate the value of a Boolean attribute.
<input type=checkbox name=vegetable value=Broccoli checked= />
<input type=checkbox name=vegetable value=Broccoli checked=checked />
In the first case, the value is not given and is assumed. Although, the latter seems like a
redundant form, it is the more preferred form, particularly if you are using jQuery.
Global Attribute Reference
There is a set of named attributes available in HTML5, which can be used with any
element. Examples of these attributes include accesskey, spellcheck and draggable, in
addition to several others.
Void Elements
Some elements do not have to contain content under any circumstance. These elements
include <link>, <br> and <area>, in addition to many others.
Self-closing Tags
If you are using an element that does not contain any content, then you can use a self
closing tag. An example of this tag is <br/>. However, please note that other tags like
<div> have to be written as <div></div> even if they do not have any content.
How to Add Expando Attributes
Any attribute that you as the author define is known as expando attribute. You can give
this custom attribute a name and assign a value to the same as and when required.
How to Add Comments
Comments can be added using a ! and two hyphens (-). The syntax for adding comments is
as follows:
<!text >
You can also add conditional comments using the following syntax:
<![if lte IE 7]> <html class=no-js ie6 lang=en> <![endif]>
This comment determines if the browser being used is an earlier version released earlier
than IE7.
How to Create HTML Document
An HTML document can simply be described as a frame that contains metadata, content
and a structure.
The HTML document must start with the tag <!DOCTYPE html>. In HTML5, the
declaration is an indication to the browser that it should work in no-quirks mode or
HTML5 compliant mode.
The next tag should be <html>. This element includes within itself the elements <head>
and <body>. The <head> element can contain the metadata for the HTML document,
which is declared and defined using the <meta> tag. The <head> element also contains the
<title> element, which serves the following purposes:
Used by search engines
breaking space character. For example, if you want to display 10 mph such that 10
and mph are not separated by a newline, you can write 10 mph.
How to Embed Content
Now that you know how to create HTML documents with your content, you may want to
embed content into the document from other sources. This content may be text from
another HTML document or flash applications.
Inline Frames
Inline frames are used to embed HTML documents inline with the content of the current
HTML document. Therefore, in a way, this element creates nested browsers as multiple
web page are loaded in the same window. These are implemented using the <iframe>
element. Nested browsing contexts can be navigated using:
window.parent This WindowProxy object represents the parent browsing
context.
window.top This WindowProxy object represents the top-level browsing
context
window.frameElement This represents the browsing context container and if
there is no context container, it returns null.
The syntax of the <iframe> element is as follows:
<iframe src=name></iframe>
Here the name of the attribute defines the name of the browsing context. This name can be
any string like sample.html. However, the strong should not start with an underscore as
names starting with underscore are reserved for special key names like _blank.
Sandboxing
Sandboxing is used for avoiding the introduction of pop-ups or any other malware in your
HTML document. In order to implement this, the attribute sandbox is used. You can use
this attribute in the following manner:
<iframe sandbox=keywords src=name>
</iframe>
When you use sandboxing, several restrictions are imposed on the called context. These
restrictions include disabling forms, scripts, plugins and any frame other than itself. You
can force the system to override these restrictions using keywords like:
allow-same-origin
allow-scripts
allow-forms
allow-top-navigation
Multiple keywords can be used by separating them using a space.
Seamless Embedding
The seamless attribute can be used in the <iframe> element for embedding of content in a
manner than this content appears to be part of the main content. This attribute can be used
in the following manner:
<iframe seamless= src=name></iframe>
<iframe seamless src=name></iframe>
<iframe seamless=seamless src=name></iframe>
This attribute may not be supported by many browsers. Therefore, you can use CSS to
achieve a similar effect.
Hyperlinks
Hyperlinks can be implemented using the <a> element. This element can be used to link
both external (a different HTML document) as well as internal (another location in the
same HTML document) links.
All links are underlined and depending upon the nature of the link, the colour of the link
changes.
Blue - Unvisited link
Purple - Visited link
Red - Active link
The first attribute of the <a> element is href, which is given the value of the URL.
Syntax for external links:
<a href=address>Text</a>
Syntax for internal links:
<a href=#id>Text</a>
The id here is the id of the tag to which you want the link to jump onto. However, if you
use only the # value, the link jumps to the top of the page.
The other attribute used with the <a> element is target, which allows you to control the
behaviour of the link. For instance, if you want the link to open in another window, then
this requirement can be specified using this attribute. The following can be used:
_blank
This opens the link in a new browser window.
_parent
This opens the link in the parent window.
_self
This opens the link in the current window and is the default setting.
_top
This opens the link in the topmost frame of the current window.
<iframe_name>
This opens the link in the <iframe> element of the specified name. This is generally
used in menus.
Hyperlinks can also be used to send emails using the following syntax:
<a href=mailto:email address>Text</a>
When the user clicks on the link, an email will be sent to the specified email address.
Embedding Images
The <img> element is used for adding images to the HTML document. The <img> tag is a
void element and does not require a closing tag.
The required tag for this element is src, which specifies the absolute or relative address at
which the image is stored. Another attribute than can be used with the <img> tag is target,
which is used to specify the text that must be displayed in case the image is not available.
Syntax:
<img src=address target=alternative text>
It is important to note that you only give references to images and they are not embedded
into the HTML document, in actuality. The allowed image formats are jpeg, svg, png and
gif.
How to Create Image Map
The <map> element can be used to create a clickable image map. In order to create a link
between the map and image, you must set a name of the map and use this name in the
usemap attribute of the img tag.
The area of the map element is defined using <area> element. This is a self-closing tag
that is used to define the area of the map. The area of the map can be set using the
attributes shape, href, alt and coords.
The shape attribute can be give the values poly, circle, rect or default. The default value
sets the area as the size of the image. The href and alt attributes are used in the same
manner as they are used in the <a> element. Lastly, the coords attribute are defined
according to the shape chosen in the following manner:
poly x1, y1, x2, y2, , xn, yn
circle x, y, radius
rect - x1, y1, x2, y2
For the polygon, the starting and ending coordinates should be the same. In case a
discrepancy in this regard exists, a closing value is added.
Example:
<img src =worldmap.gif width=145 height=126
alt=World Map usemap =#country />
<map name=country>
<area shape=circle coords=105,50,30
href=China.html alt=China />
<area shape=default href=InvalidCountry.html alt=Please Try Again />
</map>
Embedding Plug-ins
Plugins can likewise be embedded into HTML documents using <embed> and <object>
elements. Although, both these elements perform similar tasks, they were created by
different browsers. As a result, they coexist in HTML5. While the <embed> element
provides ease of use, the <object> element provides more functionality to the user. Syntax
for these two elements is given below:
The <embed> tag
<embed src=Flash.swf> </embed>
The src attribute specifies the url or address of the file to be embedded. Other attributes
that are taken by the <embed> element includes:
type - used to specify the MIME type of the content
height used to specify the content height in pixels
width used to specify the content width in pixels
As mentioned previously, some browsers may not support <embed> element. Therefore, if
you are using it in your document, you must add fallback content specification. For
instance, if you are embedding a flash file, you must redirect the user to the download link
of flash player if the browser does not support it already.
You can do this in the following manner:
<embed src=Flash.swf >
<a href=link for downloading flash player>
<img src=address of the image for download flash player
alt=Download Adobe Flash player />
</a>
</embed>
The <object> tag
The <object> tag allows you to embed a variety of multimedia files like video, audio,
PDF, applets and Flash. The element accepts the following attributes:
type used to specify the MIME type of data
form used to specify the form id or ids of the object
data used to specify the URL of the resources that the object uses
usemap used to specify the name of a client-side image map that the object
uses
name used to specify the object name
height used to specify the height of the object in pixels
width used to specify the width of the object in pixels
Of all the attributes mentioned above, it is necessary to mention either the data or type
attribute.
Data can be passed into the <object> element using the <param> tag, which is a
combination of name and value attributes. Multiple parameters can be declared using this
tag.
<object data=file.wav>
<param name=autoplay value=false />
</object>
Note:
1. The <object> element must always be used inside the <body> element.
2. HTML5 supports only the attributes listed above and global attributes for the
<object> element.
denoted by Infinity.
Infinity - any number that exceeds the value 1.7976931348623157E + 10308 is
denoted by Infinity.
String Data Type
A string can simply be described as a collection of characters. Whenever you declare
character(s) within quotes, the system interprets as a string. Sample strings include:
Hello World!
Hello World!
However, if you want to include quotes as characters in the string, then you must add \
before the character. Sample string:
You\re Welcome
You\re Welcome
JavaScript also supports other escape sequences like \t for tab and \n for newline.
Boolean Data Type
The Boolean data type is a binary data type and return either true or false. These operators
are commonly used to indicate results of comparisons. For example,
10 > 5 will give true while 5 > 10 will give false.
Operations on Number data Type
In order to perform calculations, operators are used. JavaScript supports all the basic
operators and the operator precedence is as follows:
Addition and subtraction have the same precedence.
Multiplication and division have the same precedence.
The precedence of multiplication and division is higher than that of addition and
subtraction.
If an expression contains several operators of the same precedence, then the
expression is evaluated from left to right.
In addition to the basic operators, JavaScript also supports modulo (%) operator. This
operator performs division and returns the remainder as result. For example, 23%7 is
equal to 2.
Unary Operators
While operators like addition and subtraction need to operands to execute, some operators
require only one. An example of such operators is typeof, which returns the datatype of the
data. For example, typeof 12 returns number. Please note that + and - can also be used
as unary operators. This is the case when they are used to specify the sign of a number.
Logical Operators
Three logical operators are available for use in JavaScript namely, Not (!), Or (||) and And
(&&). The results of operations that involve these operators are Boolean (true or false).
The results of these operations are computed in accordance with the following:
AND (&&) Binary operator
Both the conditions must be true
OR (||)
Binary operator
At least one of the conditions must be true
NOT (!)
Unary operator
The value of the condition determined is complemented.
For example,
Red != Blue && 5 > 1 = true
Red != Blue && 5 < 1 = false
Red == Blue && 5 < 1 = false
For conditional operators, JavaScript uses short-circuit evaluation. In other words, if the
value of the first condition is computed to be false, the system does not evaluate the
other condition and directly presents the result.
Writing Code in JavaScript
Any statement followed by a semicolon is referred to as a statement. This statement may
or may not produce a value unlike expressions, which must inadvertently produce a value.
Variables
Manipulation of data is performed with the help of variables. Data is stored in the memory
and a named reference to this memory location is called a variable. Any identifier is
declared as a variable by preceding it with a keyword var. A sample declaration of a
variable is:
var result;
This statement declares a variable result. You may also define the variable as you declare
it using a statement like this:
var result = 0;
or
var result = 23*4+6;
Certain rules have to be followed while naming variables. These rules are as follows:
A variable name can be a combination of numbers and characters.
function is to be called from within the code, parameters can be sent to the function from
the code. Upon execution, the function returns a value to the calling function. The syntax
for function declaration and definition is as follows:
function multiply(a, b){
return a*b;
}
The name of the function must always be preceded with the keyword function. The
variables a and b are parameters passed into the function and the function return the value
obtained by computing a*b. This is a simple function, but you can implement complex
and large function depending upon the functionality desired.
Now that you have implemented the function, you must be wondering as to how the
function is called. Here is an example:
var x=2;
var y=5
var c=multiply(x, y);
Here, x and y are arguments that the function multiply will receive as parameters.
JavaScript is a loosely typed language. What that means is that if you pass more
arguments to a function than what it is expecting, the system simply uses the first n
arguments required and discards the rest. The advantage of this functionality is that you
can use already implemented functions and pass the extra argument to scale the function
and add functionality to it. On the other hand, you will not be able to get any indication of
error if you unintentionally pass the wrong number of arguments.
JavaScript also provides some built-in functions for interacting with the user. These
functions are as follows:
alert
This function raises an alert with a message and the system resumes operation after
the user clicks on the OK button. Sample implementation:
alert(Alert message!);
prompt
This function presents a textbox to the user and asks him or her to give input. You
can supply the default value in the presented textbox and the user can click on the
OK button to accept that the value entered is correct. Sample implementation:
var result = prompt(Enter a value, default value);
confirm
This message gives the user the choice to OK or CANCEL an action. Sample
implementation:
var result = confirm(Do you wish to proceed?);
Function Scope
Each variable that you declare possesses a scope of operation, which is the function within
which the variable has been declared. This is called the local scope. Unlike, many other
languages, which define local scope by the curly braces within which the variable lies,
JavaScripts local scope is same as function scope.
In addition to this, JavaScript also supports the concept of global scope, in which variables
can be declared global and thus, can be used anywhere in the program.
Nesting Functions
Functions can be nested at any level. In other words, a function can be called from within
another function, which could have been called from a different function. However, it is
important to note that the scope of variable is within the function in which they are
declared.
Conversion of One Data Type to Another
The prompt function discussed in the previous function returns a string. However, you had
asked the user to enter a number. In such a scenario, a string to number conversion may be
required. In JavaScript, a variable can be converted from one type to another using the
following functions:
Number Function
This function converts the object supplied to it into number data type. However, if
the function is unable to perform the conversion, NaN is returned.
String Function
This function converts the object supplied to it into string data type.
Conditional Programming
While writing code, you will faced with several situation where you need to execute a
different set of instructions if the condition is true and another set of instructions if the
same is false.
if-else
In order to implement such scenarios, you can use the if-else construct.
Syntax:
If(condition)
{
//code
}
else
{
//code
}
Consider a scenario in which you ask the user to enter his or her age using prompt
function. Now, you must validate if the age is a valid number, before performing any
computation on the value supplied. This is an ideal scenario of implementing conditional
programming. Sample implementation for this scenario is:
var userAge = prompt(Enter your age: , );
if(isNaN(userAge))
{
alert(Age entered is invalid!);
}
else
{
//code
}
In this sample code, the if clause checks if the entered value is a number. If the condition
is true, that is the object entered is not a number, the user is given an alert message.
However, if the condition is false, the code for else is executed.
It is important to note here that for single statements, it is not necessary to use curly
braces. The above mentioned code can also be written as:
Switch
Multiple else ifs can be implemented using this construct. The execution overhead is high
for this conditional programming construct as conditions are sequentially checked for
validity. As an alternative, another keyword, switch, is available, which implements
multiple conditions in the form of a jump table. Therefore, the execution overhead for
switch is lesser than that of if-else if.
Sample implementation:
var userChoice = prompt(Choose an alphabet: a, b, c, e);
switch (userChoice) {
case a:
alert(a chosen\n);
break;
case b:
alert(b chosen\n);
break;
case c:
alert(c chosen\n);
break;
default:
alert(None of the alphabets chosen\n);
break;
};
The switch construct matches that value entered by the user with the values presented in
the cases. If a matching value is found, the case is executed. However, in case, none of the
case values match the entered input, the default case is executed. Besides this, you can
also use conditions in case values and the case for which the condition holds true is
executed.
If you do not use the break statement after the code of a case, all the cases following the
matching case will be executed. For example, if the user enters b for the above example
and there are no break statements after the case code, then the output will be:
b chosen
c chosen
None of the alphabets chosen
Also, it is a good practice to use a break statement in the default case as well.
Note:
If you wish to determine if a keyword has been assigned any value or not, you can use the
following code:
if(c)
{
//code
}
else
{
//code
}
If the variable c has been assigned a not-null value, then the if condition is true and the
corresponding code is executed. On the other hand, if the value of variable c is undefined
or null, the code within the else construct is executed.
Note:
The value of the following conditions will always be true:
== 0
null == undefined
123 == 123
false == 0;
Please note that JavaScript converts the type of the variable concerned for compatibility in
comparisons.
However, if you want to compare both the value and type of two variables, then JavaScript
provides another set of operators, === and !===. When the comparisons for the values
mentioned in the above example are done using this operator, the resultant will always be
false.
Implementing Code Loops
Looping is an important and commonly used construct of any programming language. You
will be faced by several situations when you need to perform the same set of instructions,
a given number of times. In order to implement this scenario, loops have to be used. Loop
constructs available in JavaScript include for, while and do-while.
The while loop includes a condition and as long as the condition remains true, the loop
continues to execute. The do while loop is a modification of the while loop. If the
condition in the while is false, the while loop will not execute at all. On the other hand,
even if the while condition is false, the do-while loop executes at least once.
//code
}
This loop will run 10 times.
Note:
1. If at any point in time, you wish the loop to break, you can use the break statement.
2. If you do not specify a condition or specify a condition that is always true, the loop
will run infinitely.
Error Handling
Exceptions are expected to occur at several points in your code. Therefore, it is best to
implement a mechanism that can help you deal with these exceptions and avoid crashing.
An exception can be described as an illegal operation or any condition that is unexpected
and not ordinary. A few examples of exceptions include unauthorized memory access.
You can perform exception handling at your own level by validating the values of
variables before performing any operations. for instance, before performing division, it is
advisable to check if the value of the denominator is equal to zero. Any operation that
involves division of a number by zero raises the divide-by-zero exception.
However, there are other situations that cannot be handled in this manner. For instance, if
the network connection breaks abruptly, you cannot do anything to pre-emptively handle
the situation. Therefore, for situations like these, try, catch and finally keywords are used.
The code that is expected to throw an exception is put inside the try block. This exception,
upon its occurrence, is caught by the catch block, which executes code that is supposed to
be executed immediately after an exception is caught. The catch may also be followed by
the finally block, which performs the final cleanup. This block is executed after the
execution of try and catch blocks.
Syntax:
try
{
//code
}
catch(exception name)
{
//code
}
finally
{
//code
}
Working with Objects
JavaScript allows user access to a number of existing objects. One of these objects is an
array. This section discusses all the basics related to this chapter. Dealing with objects in
JavaScript also includes creation and handling of customized objects. However, this topic
shall be covered in the chapter on JavaScript and jQuery.
Arrays
A collection of similar objects that are sequenced contiguously are referred to as an array.
This array is given a name and each element can be accessed using the indexer, in the
following form:
Let arrName[] be an array of names. The element arrName[2] refers to the third element
of the array.
An array can be created using the following three methods:
Insertion of Items Using Indexer
An array can be created using the new keyword and then, elements can be added
into the array by assigning values to independent elements of the array. The new
keyword creates an instance of the object Array using the constructor for the same.
Sample implementation:
var arrName = new Array();
arrName [0] = Jack;
arrName [1] = Alex;
Condensed Array
The second type of implementation also uses the new keyword. However, in this
case, the values are assigned to the elements as arguments to the constructor of the
Array object.
Sample implementation:
var arrName = new Array(Jack, Alex);
Literal Array
In this type of array definition, values are provided within the square brackets.
Sample implementation:
var arrName = [ Jack, Alex];
The advantage of using the first type of definition is that it allows you to assign values to
the elements anywhere in the code. On the other hand, the second and third type of
implementation requires you to have the exact list of elements with you beforehand.
There are some properties associated with all object. The one property that can come in
handy to you is length, which is a read-only value and when called return the number of
elements present in the array. You can use this property in loops and conditions.
Objects can also have their own functions. These functions are called methods. The
methods available for Array include:
concat
Returns an array, which is the concatenation of the two arrays supplied to it.
indexOf
Finds the location of the element concerned in the array and returns the index of the
same.
join
This method concatenates all the values present in the array. However, all these
values are separated by a comma by default. You can specify a delimiter of your
choice as well.
lastIndexOf
This method works similarly as indexOf. However, it performs the search from the
last element of the array. Therefore, it returns the index of the last element that
matches the specified criterion.
pop
Removes the last element and returns its value.
push
Adds the concerned element to the end of the array and returns the changed value
of length.
reverse
This method reverses the order of the array elements. The original array is modified
by this method.
shift
Removes and returns the first value. If the array is empty, then undefined is
returned.
slice
This method requires two arguments, start index and end index. A new array is
created with elements same as the elements present at indexes (start index) and
(end index 1).
sort
This method sorts the elements and modifies the original array.
splice
This method removes and adds elements to the specified array. The arguments
taken by this method are start index (index from where the system should start
removing elements), number of elements to be removed and elements to be added.
If the value passed for number of elements is 0, then no elements are deleted. On
the other hand, if this value is greater than the size of the array, all elements from
the start index to the end of the array are deleted.
toString
This method creates a string, which is a comma separated concatenated string of all
getElementById
This method returns a reference to the first element that has the specified ID.
getElementsByTagName
This method returns the active NodeList, which has the specified tag name.
getElementsByName
This method returns the active NodeList, which has the specified name. This is a
preferred method for option buttons.
getElementsByClass
This method returns the active NodeList, which has the specified class name.
However, this method is not supported by Internet Explorer version 8 and earlier.
querySelector
This method accepts CSS selector as parameter and return the first matched
element. However, this method is not supported by Internet Explorer version 7 and
earlier.
querySelectorAll
This method accepts CSS selector as parameter and return all the matched
elements. Therefore, it returns a static NodeList. However, this method is not
supported by Internet Explorer version 7 and earlier.
Events
If you look at JavaScript as an engine, then events are what give it the required spark.
Events can occur in two situations. The first type of events are those that occur during user
interactions. A user may click an image or enter text. All these are classified as events.
Besides this, changes in state of the system are also considered an event. For instance, if a
video starts or stops, an event is said to have occurred. The DOM allows you to capture
events and execute code for the same.
In JavaScript, events are based on the publish-subscribe methodology. Upon creation of an
object, the developer can publish the events that are related to this object. Moreover, event
handlers can be added to this object whenever it is used. The event handler function
notifies the subscribed events that the event has been triggered. This notification includes
information about the event like location of the mouse and key-presses, in addition to
several other details relevant to the event.
Capturing events:
There may be situations when an event may be attached to a button click, which may lie
inside a hyperlink. In this situation, there is nesting of elements. Therefore, the event,
when triggered, is passed down the DOM hierarchy. This process is called event
capturing. However, once the event has reached the element, this event is bubbled up the
hierarchy. This process is called event bubbling. This movement of the event across the
hierarchy gives the developer an opportunity to subscribe or cancel the propagation, on
need basis.
Subscribing to event:
The function, addEventListener, can be used for the subscription process. This function
requires three arguments, the event, the function that needs to be called for the event and a
Boolean value that determines if the function will be called during the capture or bubble
process (true capture, false bubble). Mostly, this value is set to false. This is the
preferred method for subscription as it is mentioned in the W3C standard.
Sample Code:
var btn = document.getElementById(btnDownload);
//download
e.stopPropagation();
}
How to prevent the default operation?
This can be done by using the function, preventDefault, in the following manner:
var hyperlink = document.getElementById(linkSave);
hyperlink.addEventListener(click, saveData, false);
function saveData(e){
//save data
e.preventDefault();
}
JavaScript also provides the this keyword, which can be used if you wish to access the
event causing element, on a frequent basis.
Window Event Reference
The current browser window is represented by the window variable, which is an instance
of the Window object. The following events are associated with this object:
afterprint
beforeonload
beforeprint
error
blur
haschange
load
message
focus
online
offline
pageshow
pagehide
redo
popstate
storage
resize
unload
undo
Form Event Reference
The actions that occur inside an HTML form trigger the flowing events:
change
blur
focus
contextmenu
forminput
formchange
invalid
input
submit
select
Keyboard Event Reference
The keyboard triggers the following events:
keyup
keypress
keydown
Mouse Event Reference
The mouse triggers the following events:
click
drag
drop
scroll
dblclick
dragenter
dragstart
dragend
dragover
dragleave
mousemove
mousedown
mouseover
mouseout
mousewheel
mouseup
Media Event Reference
Media elements like videos, images and audios also trigger events, which are as follows:
canplay
abort
waiting
durationchange
canplaythrough
ended
emptied
loadeddata
error
loadstart
loadedmetadata
play
pause
progress
playing
readystatechange
ratechange
seeking
seeked
suspend
stalled
volumechange
timeupdate
<title></title>
<style>
body {
color: white;
}
</style>
</head>
<body>
</body>
</html>
How to Create External Style Sheet
For usages where you wish to use the same style for the complete webpage or a number of
webpages, the best approach is to use an external style sheet.
This external style sheet can be linked to the HTML page in the following manner:
<!DOCTYPE html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<title></title>
<link rel=stylesheet type=text/css href=Content/mainstyle.css />
</head>
<body>
</body>
</html>
You must create a file mainstyle.css, in the Content folder, and put the style rule specified
below into the file.
body {
color: white;
}
Defining Media
It is important to note that a style sheet can contain as many style rules as you want.
Besides this, you can also link different CSS files for different media. The different media
types are as follows:
all
embossed
braille
print
handheld
speech
screen
tv
tty
The media used can be defined in the following manner:
::first-line
::after
::before
Grouping Selectors
Multiple selectors can be used for a style rule. These selectors must be separated by
commas. Sample implementation:
body, button {
color: white;
}
Using Adjacent Selectors
If you want to style the first heading in a div or any similar adjacent elements, the selector
is constructed using a plus sign (+) between the two selectors. Sample implementation:
div + h1 {
color: white;
}
Sibling Selectors
Sibling selectors are similar to adjacent selectors except for the fact that all the matching
elements are styled as against adjacent selectors, which only style the first matching
element. The selector is constructed using a ~ sign between the two selectors. Sample
implementation:
div ~ h1 {
color: white;
}
Using Attribute Selector
This selector selects all the elements for which the specified attribute exists. The selector
is written in this form:
a[title]
This selector will select all the links for which the title attribute has been specified.
Moreover, this selector type can be modified into attribute-value selector by specifying the
attribute value in the following manner:
a[title = value]
In-Built Styles of Browsers
Every browser has a built-in stylesheet, which is applied to all the webpages opened using
this browser. In fact, this stylesheet is applied before any other style sheet. You can define
your own style sheet for the browser using the Accessibility option in Tools. However,
user style sheets are browser specific. Therefore, if you open a different browser, the style
sheet you defined may not be accessible.
In case, you want your user-defined stylesheet to override any other style specified in the
HTML page, then you can use the !important modifier. This modifier sets highest
priority for the specified style statement. Sample implementation:
body {
color: white !important;
}
Cascading of Styles
The precedence and priority of the styles are decided on the basis of the following
parameters.
Importance
Specificity
Textual Order
Working with CSS Properties
Now that you are thorough with the use of selectors, the next step is to look at CSS
properties.
Color
One of the most crucial properties that are used in a web page is color, which can be
defined using ARGB, RGB and color names.
RGB value are typically defined using a decimal number, which lies between 0-255.
white #ffffff
red #ff0000
black #000000
green #008000
Color values can also be used instead of the color name. An example of how this can be
used is given below.
body {
color: #ffffff;
}
Another way to specify the color is using the RGB function, which specifies the values of
parameters using a number between 0-255 or percentage. Example of this type of
devices, which are used by many users with special needs as an alternative for reading and
comprehending web pages. Therefore, they require you to use tags that are
understandable.
How to Create HTML5 Documents
Although, the above discussion clearly mentions the importance of using meaning tags
and prohibits the use of tags like <div> and <span>, you may still have to use them for
styling purposes. As you read on, you will realize how semantic tags must be supplied for
providing meaning to your tags. It is important to mention here that semantic tags should
be used carefully, and if you realize that there is a need to define custom elements, then go
ahead and use the <div> and <span> elements. However, be sure to add an ID and classname to the elements that describe their meaning as well as possible.
How to Create HTML5 Layout Container
A layout container, as the name suggests, is a container that entails the layout of a page. In
other words, the container contains the different sections of the layout or its children in
such a manner that they can be positioned in a flexible manner. As a developer, you can
easily distinguish between <div> elements on the basis of their class names and IDs.
However, this is not true for browsers.
Therefore, there has got to be a way by which you can ask the browser to interpret
elements. For instance, you may want to ask the browser to focus on a certain <div>
element upon opening. All this and more can be done with the help of layout containers
that express elements in such a manner that they are understandable to both the browser
and the user.
Some of the commonly used elements for creating a layout container include:
<header>
It is used to define the header section or the topmost section of the HTML
document. This element can also be defined inside the <article> element.
<footer>
It is used to define the footer section or the bottom-most section of the HTML
document. This element can also be defined inside the <article> element.
<nav>
It is used to define the section that contains all the navigational links.
<aside>
This element is generally used for sidebars and separates the content in the <aside>
element from the content that is outside this element.
<section>
This element defines a part of the whole section and is named with the elements
<h1>, <h2>, <h3>, <h4>, <h5> and <h6>.
<article>
This element defines a complete unit of content, which you can copy from one
location to another. An example of such a unit is a blog post.
Using Roles
The role attribute can be declared inside the <div> and <aside> elements. The role class
hierarchy and the usage of roles for providing specific meanings, as far as far as
accessibility is concerned, is defined in WAI-ARIA (Web Accessible Initiative Accessible Rich Internet
Applications).
One of the parent role classes is the landmark role class, which defines the navigational
landmarks on the webpage. The child classes of the parent role class include:
banner
This defines website specific content that is not expected to change from one
webpage to another like headers.
application
This defines that the specified area is a web application.
contentinfo
This defines the information included in the webpage about the webpage like
copyright information. This information is mostly a part of the footer content.
complementary
This defines a section of the page that is meaningful when detached from the page
as well.
main
The main web page content is defined using this child role class.
form
This defines the area of the webpage that is expected to take webpage inputs.
search
This child role class is used to define the location on the webpage that is used for
getting the search query from the user and displaying the results of the search.
navigation
The area containing navigational links is a part of this child role class.
These roles can be used for providing meaning. However, the new elements included in
HTML5 are meaningful themselves. Yet, there are some utilities that are not available in
HTML5 and for these, role attribute can be used.
How to Control format using <div> element?
As mentioned previously, the <div> element is essentially invisible and does not provide
any meaning to the element. However, if you wish to use it for formatting purposes only,
then it is perfect for this purpose.
How to Add Thematic Breaks?
The void element <hr/> can be used for adding thematic breaks, which are used for
denoting a transition from one set of content to another.
How to Annotate Content?
There are several elements available for annotation. These include <b> and <i>, which
you have been using for ages. However, they have new meanings now, in addition to the
style that they denote. For instance, the <b> element denotes the style bold. In addition
to this, HTML5 adds the meaning span of text with special focus, but no change in mood,
importance level or implication.
Although, the use of the bold style makes more sense in this context, but you can still use
this element for denoting names of products in reviews or keywords. Similarly, the
element <strong> indicates the relative importance of content and <i> denotes a change in
mood or implication of the content concerned. Besides this, the <em> element is used for
text that will be alternatively pronounced by the reader.
How to Use <abbr> for Acronyms and Abbreviations?
In the previous versions of HTML, the <acronym> element was used for this purpose.
However, this element has become obsolete and the new element used for this purpose is
<abbr>. It is an inline element, which is generally used with other inline elements like
<samp class=maintainWhiteSpace>
This is Screen.
</samp>
The <pre> Element
It is important to mention here that these elements do not preserve the whitespace.
Therefore, a class needs to be implemented for this purpose. This class should look like:
style rule.
.maintainWhiteSpace {
white-space: pre;
}
Some browsers may not support this style rule. Therefore, for such browsers, it is
advisable to use the element <pre>. Therefore, <pre> element can be used for defining the
pre-formatting rules.
The <var> Element
This element is used to declare that the text specified inside it, is a variable. Example:
<p>
The variable <var>i</var> represents the number of iterations for the loop to perform.
</p>
The <br /> and <wbr /> Elements
The <br/> element implements a line break. On the other hand, the <wbr/> implements a
word break.
The <dfn> Element
There may be occasions when you wish to define a term. This can be done using the
<dfn> element, which takes title as one of its attributes.
Working with Figures
Images and figures are an integral part of any web page content. Therefore, every figure
can also be viewed as a unit of content, which may consist of a caption and a reference
from the page to which it may belong. In order to define one or more images, the <figure>
element is used. The element <figurecaption> can be used for defining the caption of the
figure.
However, it is important to mention here that the <figure> element does not give any
importance to the position of the figure and the same is included along with the content.
However, if the position and location of the figure is of importance to you., then you must
consider using the <div> element.
The <summary> and <details> Elements
The element <summary> contains the summary of the content of the webpage, which can
be displayed in the form of a collapsible list using the <details> element. Therefore, when
you load a page, only the contents of the <summary> element will be displayed, while the
contents of the <details> element are displayed when the user clicks on the summary.
Other Annotations
In addition to the above mentioned, there are a few more annotations available, which
include:
<s> - Used for striking out text
<u> - Used for underlining text
<mark> - Used for highlighting text
<ins> - Used for indicating that the text has been inserted
<del> - Used for indicating that the text has been deleted
<small> - used for indicating that the text must be printed in fine letters
<sub> - Indicates that the text is a subscript
<sup> - Indicates that the text is a superscript
<time> - Used for indicating that the text denotes time and date
<kbd> - used for indicating that the text is a user input
Language Elements
You may need to use characters of Chinese, Japanese or Korean origin in your text. In
order to support this inclusion, the element <ruby> can be used. Inside this element, other
elements like <bdi> and <bdo>, for defining the isolation and direction of text. Besides
this, <rt> and <rp> elements can also be used for placing notation or parentheses in the
text of <ruby> element.
Working with Lists
In HTML5, several elements for defining unordered, ordered and descriptive lists exist. A
fourth category of Custom lists is also present to allow customization by the developer.
The list items for all these are declared using the <li> element. Moreover, all the three
types of lists support list nesting.
Ordered Lists
Ordered lists are declared using the element <ol> and the elements of order in this list
are brought about by an automatic numbering of the elements that are included in this list.
The attributes that can be used with ordered lists include:
start - Used to set the starting number of the list
reversed - Used for declaring if the list has to be ordered in an ascending or
descending order
type Used for declaring the type of the list, which can be A, a, 1 or I.
Unordered Lists
This type of a list is declared using the <ul> element and there is no numbering of
elements in this case. The elements of the lists are simply represented as bullet points.
Description Lists
This type of a list is declared using the <dl> element. Using this element, you can give a
description containing zero or more terms. Besides this, the elements of the list are
declared using the <dt> element, which specifies the term, and <dd>, which gives a
description of the term.
Custom Lists
The developer can make use of CSS styles to create custom lists. In this case, a different
style rule can be created for each level of a nested list.
Working with Tables
Another format for arranging and presenting data in webpages is tables. Tables are
declared using the <table> element and represents data in a rows-columns format. The
cells of the tables are defined using the <tr> and <td> elements. While <tr> is used for
rows, <td> is used for columns.
Despite that fact that HTML5 tables are one of the most powerful constructs available to
the developer, it is important to understand how and where tables can be most
appropriately used. Here are the reasons why tables should not be used:
The table created for a web page is not rendered until the </table> tag is read. On
the other hand, if the same construct is created using the <div> element, the content
will be rendered as it is read.
1978
David
1967
Alex
1989
As you observe the table, you must have realized that the table is not complete unless we
define what is called the table headers. This can be done using the <th> element. You can
create table headers both vertically and horizontally. For example, we can define the
following table header in the code used above.
<table>
<tr>
<th>Name</td>
<th>Year of Birth</td>
</tr>
<tr>
<td>Franky</td>
<td>1978</td>
</tr>
<tr>
<td>David</td>
<td>1967</td>
</tr>
<tr>
<td>Alex</td>
<td>1989</td></tr>
</table>
The resulting table for this code will look like this:
Name
Year of Birth
Franky
1978
David
1967
Alex
1989
In the above case, the table headers are simply of a larger font size. However, you can
style these as you want by style rules. This can be done in the following manner:
th {
background-color: black;
color: white
}
This style rule will color the cells of the table headers with black color and the text will be
written in white.
The normal behavior of most browsers is to automatically place all the <tr> elements in
the <tbody>, indicating that this is the body of the table. However, it is a good practice to
define the <tbody> explicitly. Besides this, the <thead> and <tfoot> can also be explicitly
defined. The header, body and footer of the table can be individually styled using CSS. As
a rule, you can have one header element, one or more body elements and one footer
element.
The next important content feature that must be added to the table created above is the
table caption. In order to define the table caption, the <caption> element is used.
In some cases, you may feel the need to style individual columns. This may seem like a
difficult task considering the fact that tables are essentially row centric in HTML. While
you have <tr> elements to identify rows, there are no <tc> elements for identifying
columns. The <td> element identifies a cell. However, you can still style individual
columns using the <colgroup> or <col> elements. The <table> element can have the
<colgroup> element, which includes the <col> elements for columns that are a part of this
group of elements. In addition, the <col> element also has a span attribute for defining the
columns that are a part of this group. A sample implementation to explain how this works
is given below:
<colgroup>
<col span=2 class=vHeader />
</colgroup>
The CSS style rule for styling this group of columns can be something like this
.vHeader {
color: red;
}
While the tables discussed till now are regular tables, HTML5 also supports irregular
tables, which can be described as tables that have a different number of columns for each
row. The rowspan and colspan attributes can be used for managing the layout of the table.
};
This code creates an object customer1, with the data members, name and yearOfBirth and
the member function getCustomerInformation. It is also important to note the use of this
keyword, which accesses the values correctly being used or referenced for the object
concerned.
Besides this, you can also create objects dynamically using the new keyword. The
methods inherited include:
constructor
isPrototypeOf
hasOwnProperty
toLocalString
propertyIsEnumerable
valueOf
toString
Once the object has been created, properties can be added to the same in the following
manner:
function getCustomer(myName, myYearOfBirth) {
var newCust = new Object();
newCust.name = myName;
newCust.yearOfBirth = myYearOfBirth;
newCust.getCustomerInfo = function () {
return Customer: + this.name + + this.yearOfBirth;
};
return newCust;
}
This code creates an object newCust dynamically. Several instances of this can be created
in the following manner:
var cust1 = getCustomer (Alex, 1978);
var cust2 = getCustomer (David, 1986);
Although, JavaScript doesnt support a particular keyword class, but you can simulate
classes using the method mentioned above.
Namespaces
There is no specific keyword like namespace for implementing namespaces. However,
namespaces can be implemented using the concepts of classes and objects. If you classify
variables and methods into objects and access them as instances of these objects, then you
are placing only the names of these objects in the global namespace, reducing the scope of
the variables to the object that they belong.
Implementing Inheritance
You can define is-a relationships between objects in JavaScript by creating objects and
then classifying those objects on the basis of their common characteristics. For instance, if
you are implementing an object for employee of a company. You can create objects for
specific types of objects like managerTechnical, managerGeneral, technicalStaff,
recruitmentStaff and officeStaff and then classify them into objects, technicalStaff, which
includes the objects managerTechnical and technicalStaff, and adminStaff, which includes
the managerGeneral, recruitmentStaff and officeStaff. In a similar manner, new functions
can also be defined.
DOM.
Effects are methods that can be used for creating animations for your webpage.
Events are methods used to perform event-based execution.
Selectors are methods that can be used for accessing elements of DOM in CSS
selectors.
Offset are methods that are used to position the DOM elements.
Utilities, which is a group of utility methods
Before getting to use jQuery, you will need to include it into your project. Once you have
installed it and you are ready to use it to your project, the next step is to learn how to use
it.
First things first, you need to reference the jQuery library on the webpage that needs to use
it in the following manner:
<script type=text/javascript src=Scripts/qunit.js></script>
<script src=Scripts/jquery-1.8.2.js></script>
The next thing to know is that the jQuery code that you are hoping to use in your HTML
page lies in the jQuery namespace, which has an alias $. Therefore, you can write either
jQuery.jFeature or $.jFeature when referring to a feature of jQuery.
Before, you can start using it in your webpages, you will also need to change the default.js
file as follows:
function initialize() {
txtInput = $(#txtInput);
txtResult = $(#txtResult);
clear();
}
This allows you to use jQuery and CSS selectors by matching them using their IDs.
Also, as you move ahead with coding using jQuery, remember to refresh the screen using
Ctrl+F5 after making any changes as the browser may not be able to catch the JavaScript
modification right away. Moreover, use jQuery objects as much as possible because the
cross-browser compatibility that they offer.
A DOM object can be referenced from a jQuery wrapper in the following manner:
var domElement = $(#txtInput)[0];
Here is a simple code that checks if the element exists before referencing it.
var domElement;
if($(#txtInput).length > 0){
domElement = $(#txtInput)[0];
}
How to Create a jQuery wrapper for Referencing a DOM element
A jQuery wrapper can be created from a DOM element reference in the following manner:
var myDoc = $(document);
var inText = $(this).text();
The first statement assigns the wrapped object to the variable. On the other hand, the
second statement wraps the object referenced using this.
How to Add Event Listeners
jQuery provides the .on method for subscribing to events. Besides this, you can
unsubscribe using the .off method. These methods can be used in the following manner:
$(#btnSubmitInfo).on(click, onSubmit);
$(#btnSubmitInfo).off(click, onSubmit);
How to Trigger Event Handlers
JQuery provides triggers or the method, triggerHandler, for triggering event handlers or
handler code execution. This can be done in the following manner:
$(#btnSubmitInfo).triggerHandler(click);
Initialization Code
You will often be faced with the requirement to run an initialization code upon the loading
of an HTML document. You can do this using jQuery in the following manner:
<script>
$(document).ready(function () {
initializationFunction();
});
</script>
This can be placed at the bottom of the HTML document. It will call the
initializationFunction.
CHAPTER 7: FORMS
In the previous chapters, you have already studied how HTML documents can be created
and manipulated. Taking a lead from them, we can now move on to understanding forms,
which is one of the most crucial and commonly used units of content in webpage
development.
Simply, a form is a way in which data is collected and sent to a location where it can be
processed, which is a server in most cases. However, since we are yet to discuss server
side scripting, we will focus on sending the data to an email address. However, it is
important to note that we do not recommend this practice and it is used only for
understanding purposes.
Web Communications
Before moving to the working of forms, it is important to know some basics of HTTP. A
typical web communication consists of the following activities:
1. When a user browses a webpage, a request for access to a web server resource is
initiated by sending a GET HTTP request.
2. The request is processed by the server and sends a response to the browser using
HTTP protocol.
3. The browser process the server response and presents it to the user in the form of a
form.
4. The user enters inputs to the form and upon hitting the submit or enter button, this
data is sent to the server, using HTTP protocol again.
5. This data is again processed by the server and the server posts its response to the
browser, which is displayed on the webpage.
Web Servers
Originally, web servers were designed to receive and process requests and send the results
back to the browser using HTTP. Initially, when the web pages were simple, such web
servers were able to process a good number of requests per unit time. There were no states
involved as sending and receiving requests were as simple as opening a connection,
transferring data and closing the connection.
The new age web servers are much more equipped that these simple web servers. The web
servers of today implement what is called the keep alive features, which ensures that the
connection remains open for a definite period of time in which subsequent requests by the
same browser to the server can be entertained.
Web Browsers
The web browser is a desktop application, which displays web pages and manages user
interactions between the webpages and the server. The communication between the web
servers and pages is established using technologies like AJAX and Asynchronous
JavaScript.
How is Data Submitted to the Web Server
An HTML form can be created using the <form> element in the following manner:
<form method=post action=getCustomerInformation.aspx >
Enter Customer Number:
<input type=text name=Number />
<input type=submit value=Get Customer Information />
</form>
This form takes in the customer number and returns a page that displays the details of the
customer.
However, it is important to note that not all elements can be used for submitting data in a
form. The allowed elements for this purpose are:
<textarea> - It takes a multi-line input
<button> - It is a clickable button, which can be placed on any content or image.
<select> - It is a drop-down list, which allows multiple selections. The selected
options can be identified using jQuery: $(option:selected)
<input type=checkbox> - It is a checkbox, which has a value attribute used for
setting as well as reading the status of the checkbox. The jQuery for identifying
checked checkboxes is: $(input[type=checkbox]:checked)
<input type=button> - It is a clickable button, which has a text prompt.
<input type=datetime> - It is a control, which is date and time (UTC).
<input type=date> - It is a control, which is date-only.
<input type=email> - It is a file-select field, which has a browse button for
uploading a file.
<input type=color> - It is a color picker.
<input type=hidden> - It is a hidden input field.
<input type=datetime-local> - It is a control, which is date and time (any
timezone).
<input type=month> - It is a month-year control.
<input type=image> - It is an image submit button.
<input type=password> - It is a password field, with masked characters.
to address this issue, HTML5 provides an attribute, id, which can be set for multiple form
elements. This shall allow data collection from different form elements in one go.
How to Trigger Form Submission
Upon triggering, all the data collected from the submission elements of the form or forms
of the same id is sent to the server using an HTTP method. The <input> element can be
used for triggering form submission. Besides this, you can also use JavaScript for this
purpose. In order to implement this, you must give an id to the concerned form,
myFirstForm. The default.js file, which is linked to the HTML document, must contain the
following code:
$(document).ready(function () {
$(#myFirstButton).on(click, submitMyFirstForm);
});
function submitMyFirstForm() {
$(#myFirstForm).submit();
}
If the method attribute of the form is not given any value, then it is set to a default GET.
Moreover, the action attribute will also have a default value. Therefore, the button click
will reference the page to same page. However, the URL will now include a QueryString,
which is a combination of values selection or entered by the user. For instance, if the form
requests the user to enter Customer ID and the user enters 1245, then the QueryString will
be:
customerID=1245
This QueryString will be appended to the URL in the following manner:
Mywebpage.asp? customerID=1245
It is also important to mention here that the QueryString is URI encoded. Therefore,
special characters are represented by specific values. For instance, a space is represented
as + and exclamation mark (!) as %21. Name-value pair is represented as name=value
and name-value pairs are separated by &.
How to Serialize the Form
You can serialize the form using the jQuery serialize method. This can be done in the
following manner:
var myFormData = $(#myFirstForm).serialize();
You can decode the URI-encoded string using the following:
var data = decodeURIComponent(myFormData);
Using Autofocus Attribute
By default, the focus is not set to any element of the form. However, you can set focus
using the focus method, which can be implemented in the following manner:
$(input[name=firstName]).focus();
However, you can also set autofocus in the following manner:
<input type=text name=firstName autofocus=autofocus/>
How to Use Data Submission Constraints
A form can send data only if it satisfies the following constraints:
Name attribute must be set.
You must have set the value of form submission element.
The <form> element must have its form submission element defined and form
handling is done from these functions. In this example, the response function ends by
writing Hello World on the screen.
The function createServer, returns a server object, which call the function, listen. This
function listens at the port 8080 and the IP address of the host is set to 127.0.0.1. therefore,
if there is a network adapter installed on your system, your web server will start listening
to web requests rights away. The last line prints a line on the screen to let the user know
that the server is running and listening to requests.
Once you have created the file and saved the contents of the file as mentioned above, you
must open the command prompt and write the command:
Node myFile.js
Now, keeping the command prompt active, you must open the web browser and type the
address: http://localhost:8080/
As soon as the request is sent and a response is received, the same is communicated to the
user using the console window. If you have been able to do this successfully, then you
have just created your first node.js website. If you wish to stop the running of the code,
you can just press Ctrl+C.
Now that you know how requests are received, it is time to look at how these requests are
processed and responses are generated. You may need to use the url module for parsing
the QueryString.
The code mentioned below shows how you can parse the URL string and generate a
response in accordance with it.
var http = require(http);
var url = require(url);
http.createServer(function (request, response) {
exports.start = start;
when you save this piece of code in a file, a module is created. This module can be used
by other functions using the require function. For instance, if the file is saved as
sample1.js, then the start() can be used in another function using:
var samplex = require(./sample1.js);
sample1.start();
How to Create a Node.js package
A collection of modules is referred to as an application. Once you have published your
package, it can be installed and used. Consider for example, a package of different
mathematical modules.
The root folder must have the following:
README.md
\packName
\lib
main.js
\bin
mod1.js
mod2.js
Creating Aggregate Module
You may wish to make only one object for the user to access. The user should be able to
access all the modules of the package through this object. In order to accomplish this, a
main.js module must be created in the bin folder that must define the modules to be
included in the module.exports.
},
repository: ,
keywords: [
sample,
example,
add,
sub
],
author: XYZ,
license: ABC
}
In addition to test scripts, you can also give git scripts, which are the best available source
control managers.
How to Publish a Package
As mentioned previously, a package can be defined in terms of a folder structure. When
you publish your package, you make it accessible to all users. In order to perform this
operation, you must use the npm command, which is also the command used for searching
and installing packages. However, you shall be required to create an account for yourself
before publishing any of your packages using the command: npm adduser. After you enter
the required information, your account is created. However, what this also means us that
there is no validation required. Therefore, anyone can add code to the repository.
Therefore, you should be careful while downloading and installing packages from the
registry.
In order to publish a package, you simply need to go to the root directory of the package
and enter the command npm publish in the command prompt.
How to Install and Use the Package
A package that is published can be downloaded and installed by any user. You simply need
to go to the folder and give the command, npm install samplePackage. This installs the
package locally. On the other hand, if you wish to install the package globally, you can
give the command, npm install g samplePackage. For a global installation, you will need
to create a link from each application to the global install using the command, npm link
samplePackage.
The route to a global install is a junction. You can get into the node_modules folder and
back using the cd command. Once you are inside the folder, you can give the command:
npm install contoso, to initiate an install. You can now write some code that uses the
package. A sample is given below:
var samplePackage = require(samplePackage);
var finalResult = 0;
console.log();
finalResult = samplePackage.add (5,10);
console.log(add (5,10) = + finalResult);
console.log();
result = samplePackage.sub (50,10);
console.log(sub(50,10) = + finalResult);
console.log();
console.log(done);
This code tests the modules of the package. You can execute the code using:
node main
The package can be uninstalled locally using:
npm uninstall samplePackage
However, if you wish to uninstall the package globally, you can do it using
npm uninstall -g samplePackage
How to Use Express
1. The first step is to install Node.js and create a sample for keeping all .js files and
projects. Besides this, you must also install Express, which is a web application
framework for Node.js development.
2. You can create a package using the following set of commands and instructions.
1. npm init
2. You can create myPackage.js file containing the following contents:
{
name: Sample,
version: 0.0.0,
description: This is a sample website.,
main: main.js,
scripts: {
test: echo \Error: Test not specified\ && exit 1
},
repository: ,
author: XYZ,
license: BSD
}
private: true,
dependencies: {
express: 3.0.0
}
}
In order to use the file in Express, dependencies have to be added.
Moreover, if you do not define it to be private, you may get an error from
the firewall of your computer as it tries to load the page.
3. Give the install command: npm install
4. You can use the command, npm ls, to see if the package is present in the
registry.
3. You can create a simple application using the following set of instructions:
1. Create a file myApp.js and add the following to the file:
var express = require(express);
var app = express();
2. You can define the route using the myApp.Method() syntax.
app.get(/, function(request, response){
response.send(Hey World!);
});
The code mentioned above will send the response Hey World! as and when
a request is received.
3. The last section of code that must be added is for listening to the request.
This code is as follows:
var port = 8080;
app.listen(port);
console.log(Listening on port: + port);
4. Once the file is complete, you can save it and run it using the command,
node app. So, now if you open the browser and enter the address
http://localhost:8080/, you will get the response Hey World!.
4. You can add webpages to applications by replacing the app.get statement with
app.use(express.static(__dirname + /public)); This will allow you to use the same
code for a number of webpages. Sample implementation of this concept is given
below:
<!DOCTYPE html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<title></title>
</head>
<body>
<form method=get action=/submitHey>
Enter First Name: <input type=text name=firstName />
<input type=submit value=Submit />
</form>
</body>
</html>
Please note that the action attribute is set to /submitHey. In other words, this
resource is called at the server for handling the data that is passed to it using the
QueryString. The myApp.js file should contain the following:
var express = require(express);
var app = express();
app.use(express.static(__dirname + /public));
app.get(/SubmitHey, function (request, response) {
response.writeHead(200, { Content-Type: text/html });
response.write(Hey + request.query.userName + !<br />);
response.end(Enjoy.);
console.log(Handled request from + request.query.userName);
});
var port = 8080;
app.listen(port);
console.log(Listening on port: + port);
The app can be run in the manner mentioned above.
5. The formidable package can be used for posting back data. While the previous
method used the GET method, this method uses the POST method.
1. To illustrate how it works, create an HTML as follows:
<!DOCTYPE html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<title></title>
</head>
<body>
<form method=post action=/SubmitHeyPost>
Enter Name: <input type=text name=firstName />
<input type=submit value=Submit />
</form>
</body>
</html>
2. Now, in the command prompt, you need to a give a command for retrieving
the formidable package, which is:
npm info formidable
3. You can also modify the package.jason file in the following manner:
{
name: HelloExpress,
version: 0.0.0,
description: Sample Website,
main: index.js,
scripts: {
test: echo \Error: test not specified\ && exit 1
},
repository: ,
author: XYZ,
license: BSD,
private: true,
dependencies: {
formidable: 1.x,
express: 3.0.0
}
}
4. Now you can install the formidable package by typing the following
command into the command prompt:
npm install
This command installs the package locally. Therefore, you will need to add a
line to myApp.js that allows the file to reference the package:
var formidable = require(formidable);
5. A sample myApp.js file shall look like this:
var express = require(express);
var app = express();
var formidable = require(formidable);
6. You can now run the application in a similar manner as you did for the
previous example.
Working with web services
One of the biggest drawbacks of a typical website scenario is that the HTML page is
repainted even if the new page is the same as the previous page. This causes you to lose
bandwidth and resources. This drawback can be addressed using web services, which can
be used for sending and receiving data, with the benefit that the HTML page is not
repainted. The technology used for sending requests is AJAX or Asynchronous JavaScript
and XML. This technology allow you to perform the data sending operation
asynchronously.
Before moving any further, it is important to know the basics of web services and how
they can be used. A client needs to communicate with the web server on a regular basis
and this communication is facilitated by the web service. In this case, the client can be
anyone from a machine using the web service to the web service itself. Therefore, the
client, regardless what it is, needs to create and send a request to the web service, and
receive and parse the responses.
You must have heard of the term mashups, which is a term used to describe applications
that pierce together web services. Two main classes of web services exist, which are
arbitrary web services and REST or representational state transfer. While the set of
operations are arbitrary in the first case, there exists a uniform operations set in the
second.
Representational State Transfer (REST)
This framework uses the standard HTTP operations, which are mapped to its create,
delete, update and retrieve operations. Moreover, REST does not focus on interacting with
messages. Instead, its interactions are focused towards stateless resources. This is perhaps
the reason why REST concept is known for creation of clean URLs. Examples of REST
URLs include http://localhost:8080/Customers/2, which deletes a customer and
http://localhost:8080/Vehicles?VIN=XYZ12, which is used to retrieve the information
about a vehicle for which a parameter is passed using GET method.
Some firewalls may not allow the use of POST and GET methods. Therefore, it is
advisable to use verb in the QueryString. An example of how the URL will look like is:
http://localhost:8080/Vehicles?verb=DELETE&VIN=XYZ987
The HTTP protocol also allows you to implement security using the HTTPS version.
REST provides several benefits like easy connection, faster operation and lesser
consumption of resources. However, many developers prefer to use JSON or (JavaScript
Object Notation) because it is compact in size. Besides this, REST only supports GET and
POST, which restricts its capabilities, Therefore, some developers switch to RESTFUL.
Arbitrary Web Services
This type of web services is also referred to as big web services. An example of such
services is WCF or Windows Communication Foundation. Arbitrary web services expand
their realm of operations by not mapping their operations to only aspects of the protocol.
As a result, they provide more functionality, which include many security mechanisms and
message routing.
This type of web services possess a typical interface format, which can be used by the
client for reading and parsing information. As a result, the client can make calls to the
service immediately. A common API format is the Web Services Description Language
(WSDL). In case of arbitrary web services, the client must assemble its request with the
help of a SOAP (Simple Object Access Protocol) message. This web service does not use
the HTTP protocol and instead uses the TCP.
How to Create RESTful Web Service using Node.js
In the example mentioned previously, the samplePackage can be exposed as web service.
The GET method can be used on the package and the operation is passed as a parameter. A
good RESTful implementation of this package can look something like this:
http://localhost:8080/samplePackage?operation=add&x=1&y=5
How to Use AJAX to Call Web Service
Web services can be called asynchronously using AJAX, which is in actuality a
JavaScript. Instead of making a call to the server and repainting the HTML document,
AJAX just calls back to the server. In this case, the screen is not repainted. A sample
implementation is given below:
A MyPage.html can be created with the following code:
<!DOCTYPE html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<title></title>
<script type=text/javascript src=/scripts/jquery-1.8.2.min.js></script>
<script type=text/javascript src=/scripts/default.js></script>
</head>
<body>
<form id=myForm>
Enter Value of X:<input type=text id=x /><br />
Enter Value of Y:<input type=text id=y /><br />
Result of Operation: <span id=result></span><br />
$(document).ready(function () {
$(#btnAdd).on(click, addTwoNum)
});
function addTwoNum() {
var x = document.getElementById(x).value;
var y = document.getElementById(y).value;
var result = document.getElementById(finalResult);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(GET, /add?x= + x + &y= + y , false);
xmlhttp.send();
var jsonObject = JSON.parse(xmlhttp.response);
result.innerHTML = jsonObject.result;
}
The code extracts the x and y values from the <input> element for the same. After this, the
XMLHttpObject is created and the open method is called using the QueryString. After the
execution of the send function, the response string is parsed. In order to test the page, you
can give the command:
node app
This command starts the web service, after which you can open the browser window with
the link:
http://localhost:8080/SamplePage.html
this code is operational now. However, you may wish to perform the AJAX call in an
asynchronous manner. For this, you must locate the open method and change the false
parameter to true. Besides this, you will also be required to subscribe to
onreadystateschange for managing asynchronous call. This can be implemented in the
following manner:
function addTwoNum () {
var a = document.getElementById(a).value;
var b = document.getElementById(b).value;
var result = document.getElementById(finalResult);
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsonObject = JSON.parse(xmlhttp.response);
result.innerHTML = jsonObject.result;
}
}
xmlhttp.open(GET, /add?a= + a + &b= + b , true);
xmlhttp.send();
}
The codes for states are as follows:
0 - Uninitialized
1 - Loading
2 - Loaded
3 - Interactive
4 - Completed
If progress events are provided by the server, you can subscribe to the browsers progress
event. Then, an event listener can be added to initiate the execution of the code when the
event is triggered. This can be done in the following manner:
function addTwoNum () {
var a = document.getElementById(a).value;
var b = document.getElementById(b).value;
var finalResult = document.getElementById(finalResult);
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsonObject = JSON.parse(xmlhttp.response);
result.innerHTML = jsonObject.result;
}
}
xmlhttp.addEventListener(progress, updateProgress, false);
xmlhttp.open(GET, /add?a= + a + &b= + b , true);
xmlhttp.send();
}
function updateProgress(evt) {
if (evt.lengthComputable) {
type: GET,
cache: false,
dataType: json,
success: function (data) {
$(#result).html(data.result);
}
});
}
The variable values are retrieved and supplied to the ajax call. The other properties and
objects are set as shown. Another topic of concern is CORS, or Cross-Origin Resource
Sharing, which is a way for allowing cross-site AJAX calls.
approach is that the connection remains open and as and when the data is available, it is
immediately sent. However, the connection may be timed out and a re-establishment may
be required.
Understanding WebSocket Protocol
The WebSocket protocol allows bi-directional connection establishment and is designed
for implementation for web servers and clients. This technology only interacts with the
handshake and is interpreted as a transition from the http to WebSocket. The frame of this
protocol has no headers and an overhead of 2 bytes. It is a lightweight technology and its
high performance allows communication of live content and games.
Defining WebSocket API
The heart of the WebSocket technology is WebSocket object. This object is defined on the
window object and contains the following members:
close
WebSocket constructor
binaryType
send
extensions
bufferedAmount
onerror
url
onclose
onopen
onmessage
readyState
protocol
How to Implement WebSocket Object
The WebSocket communication is based on TCP and thus, it operates on port number 80.
You can use any HTML form for demonstration. The WebSocket url is of the ws:// and
wss:// form. In order to implement this form, you need to create an object of the
WebSocket API, which can then be configured using onopen, onclose, onerror and
onmessage events. The default.js file must be modified in the following manner:
/// <reference path=_references.js />
var wsUri = ws://echo.websocket.org/;
var webSocket;
$(document).ready(function () {
if (checkSupported()) {
connect();
$(#btnSend).click(doSend);
}
});
function writeOutput(message) {
var output = $(#divOutput);
output.html(output.html() + <br /> + message);
}
function checkSupported() {
if (window.WebSocket) {
writeOutput(WebSocket is supported!);
return true;
}
else {
writeOutput(WebSockets is not supported);
$(#btnSend).attr(disabled, disabled);
return false;
}
}
function connect() {
webSocket = new WebSocket(wsUri);
webSocket.onopen = function (evt) { onOpen(evt) };
webSocket.onclose = function (evt) { onClose(evt) };
webSocket.onmessage = function (evt) { onMessage(evt) };
webSocket.onerror = function (evt) { onError(evt) };
}
function doSend() {
if (webSocket.readyState != webSocket.OPEN)
{
writeOutput(NOT OPEN: + $(#txtMessage).val());
return;
}
writeOutput(SENT: + $(#txtMessage).val());
webSocket.send($(#txtMessage).val());
}
function onOpen(evt) {
writeOutput(CONNECTED);
}
function onClose(evt) {
writeOutput(DISCONNECTED);
}
function onMessage(evt) {
writeOutput(RESPONSE: + evt.data);
}
function onError(evt) {
writeOutput(ERROR: + evt.data);
}
When you create a WebSocket object, a connection to URI is automatically initiated. The
function, connect, subscribes the object the events mentioned above. The message is sent
using the doSend function. However, the readyState property of the object is checked
before sending any message. The values of the property can be as follows:
Closed = 3
Closing = 2
Open = 1
Connecting = 0
Upon reception of message from the server, the client calls the onMessage function, which
passes the event object. The property, data, of this object, contains the message sent from
the server. On the contrary, if an error has occurred, then the onError function is called and
the data property contains the reason for error generation. In some cases, the data property
may be undefined. This is particularly the case in scenarios where the connection has
timed-out.
How to Deal with Timeouts
A timeout is identified when no activity is triggered for as long as 20 minutes. One of the
most effective ways to deal with timeouts is sending an empty message to the server on a
periodic basis. This can be done in the following manner.
/// <reference path=_references.js />
var wsUri = ws://echo.websocket.org/;
var webSocket;
var timerId = 0;
$(document).ready(function () {
if (checkSupported()) {
connect();
$(#btnSend).click(doSend);
}
});
function writeOutput(message) {
var output = $(#divOutput);
output.html(output.html() + <br /> + message);
}
function checkSupported() {
if (window.WebSocket) {
writeOutput(WebSockets supported!);
return true;
}
else {
writeOutput(WebSockets NOT supported);
$(#btnSend).attr(disabled, disabled);
return false;
}
}
function connect() {
webSocket = new WebSocket(wsUri);
webSocket.onopen = function (evt) { onOpen(evt) };
webSocket.onclose = function (evt) { onClose(evt) };
webSocket.onmessage = function (evt) { onMessage(evt) };
webSocket.onerror = function (evt) { onError(evt) };
}
function keepAlive() {
var timeout = 15000;
if (webSocket.readyState == webSocket.OPEN) {
webSocket.send();
}
timerId = setTimeout(keepAlive, timeout);
}
function cancelKeepAlive() {
if (timerId) {
cancelTimeout(timerId);
}
}
function doSend() {
if (webSocket.readyState != webSocket.OPEN)
{
writeOutput(NOT OPEN: + $(#txtMessage).val());
return;
}
writeOutput(SENT: + $(#txtMessage).val());
webSocket.send($(#txtMessage).val());
}
function onOpen(evt) {
writeOutput(CONNECTED);
keepAlive();
}
function onClose(evt) {
cancelKeepAlive();
writeOutput(DISCONNECTED);
}
function onMessage(evt) {
writeOutput(RESPONSE: + evt.data);
}
function onError(evt) {
writeOutput(ERROR: + evt.data);
}
How to Handle Disconnects
Connections may sometime close errors concerning the network. Therefore, you may be
required to call the connect function while you are inside the construct of the onClose
function. This may lead to issues. A common problem faced in this regard is that the
server is unable to identify that this new connection request belongs to a client that already
exists. You can handle this issue by sending an identification message to the server.
How to Deal with Web Farms
You may need to run your application in web farm, in which case the incoming requests
may be handled by many servers. Multiple server are used to perform load balancing and
performance optimization. In this case, you can implement sticky servers, which ensures
that a client is served by only one server. In other words, all the requests by one client are
processed by the same server. This helps you to address issues that may arise due to open
connections. These problems can also be handled using products like Microsoft App
Fabric Caching Service and Redis.
How to Use WebSocket Libraries
Dealing with all the issues mentioned above can be challenging. Therefore, the best way
to deal with the situation is to use server and client libraries. There are several libraries
like SignalR and Socket.IO are available for you to explore.
$.cookie(fName, Clark);
var fName = $.cookie(fName);
Although, cookies are extensively used, they pose some serious issues like overhead and
capacity limitation. Some alternatives available include:
Google Gears
User Data
Java Applets
Flash Player
These options are better cookies and addresses its limitations, but they suffer from some
issues, which include:
Pug-in is required
User can block them
Not useful for corporate users
They are vendor-specific.
HTML5 Storage
Although, these tools offers some good, yet potentially improvable solutions, HTML5 has
come up with some innovative tools in their attempt to provide a comprehensive solution.
Web storage
The simplest form of storing data over the web is web storage, in which key-value
pairs are stored.
Web SQL database
If the application requires a full relational database, then Web SQL database is also
function isWSSupported() {
return localStorage in window;
}
if (isWSSupported()) {
localStorage.setItem(fName, $(#fName).val());
}
In addition to this, you can also use the Modernizr javascript library for the checking
purpose. It is worth mentioning here that this type of web storage allows 5MB of web
storage, as against the 4KB of web storage allowed by cookies. When the storage space is
exceeded, an exception for the same is thrown by the system.
Although, web storage allows storage of strings only, but you can also store complex
objects with the help of JSON utility methods.
How to Use Short-term Persistence with sessionStorage
SessionStorage is also a Storage Object, with the same attributes and methods as
localStorage. The only difference between the two storage mechanisms is that
sessionStorage stores data for the session only. If the browser window is closed, the data
will no longer be available for use.
Issues Related to sessionStorage and localStorage
Some of the issues related to these two storage mechanisms include:
1. Simultaneous reading and writing of the hard drive.
2. Slow searching capabilities.
3. No support for transactions.
How to Handle Storage Events
creationCallback
How to Manipulate Database
Manipulation scripts in SQL can be given as parameter to the transaction.executeSql()
function for manipulating the database. An example of how a table can be created is given
below:
transaction.executeSql(CREATE TABLE IF NOT EXISTS employees( +
id INTEGER PRIMARY KEY AUTOINCREMENT, +
fName TEXT, +
lName TEXT, +
));
IndexedDB
Till now, you have dealt with the two extreme cases of storing data over the web. While
the web storage mechanisms follows the simple key-value pair model, Web SQL is based
on the relational database model. IndexedDB is an alternative model that lies somewhere
in between these two models. It is capable of dealing with everything from strings to
complex objects. However, while using this storage mechanism, use browser-independent
methods.
A database can be opened or created in the following manner:
var indexedDB = window.indexedDB;
var openRequest = indexedDB.open(Lib, 1);
An important facet of IndexedDB object is the keypath property, which is used to define
the attributes that must be used as the key. If the specified property does not exist, then an
auto-incrementing or auto-decrementing attribute can be created.
var dbase;
openRequest.onsuccess = function(response) {
dbase = openRequest.result;
addEmployee();
};
function addEmployee() {
var myTrans = dbase.transaction(employees, readwrite);
var employees = myTrans.objectStore(employees);
var request = employees.add({fName: Sammy, lName: Carnie});
request.onsuccess = function(response) {
alert(ID of New Record = + request.result);
};
request.onerror = function(response);
}
3. A record can be updated in the following manner:
var openRequest = indexedDB.open(Lib, 1);
var dbase;
openRequest.onsuccess = function(response) {
dbase = openRequest.result;
updateEmployee();
};
function updateEmployee() {
var myTrans = dbase.transaction(employees, readwrite);
var employees = myTrans.objectStore(employees);
var request = employeess.put({fName: Sammy, lName: Carnie}, 1);
request.onsuccess = function(response) {
alert(ID of Updated Record = + request.result);
};
request.onerror = function(response);
}
4. You can delete a record using the following code:
function deleteEmployee() {
var myTrans = dbase.transaction(authors, readwrite);
var employees = myTrans.objectStore(employees);
var request = employees.delete(1);
request.onsuccess = function(response);
request.onerror = function(response);
}
How to Work with FileSystem API
The above mentioned storage mechanisms can be used for storing data, which is in form
of strings. However, complex data structures like files and images may also be stored
using URIs. However, it is an excessively costly option. HTML5 offers a low cost and
simpler option called FileSystem API for this purpose. Using this format, you can create
APPENDIX
<a> Hyperlink
<abbr> Abbreviation
<address> Contact information
<area> Image map region
<article> Independent section
<aside> Auxiliary section
<audio> Audio stream
<b> Bold text
<base> Document base URI
<bb> Browser button
<bdo> Bi-directional text override
<blockquote> Long quotation
<body> Main content
<br> Line break
<button> Push button control
<canvas> Bitmap canvas
<caption> Table caption
<cite> Citation