Html5 Interview Questions
Html5 Interview Questions
SGML (Standard generalized markup language) is a standard which tells how to specify document markup. It’s only a
Meta language which describes how a document markup should be.
So by SGML they created DTD (Document Type Defination) which the HTML refers and needs to adhere to the same.
So you will always find “DOCTYPE” attribute at the top of HTML page which defines which DTD is used for parsing
purpose.
Now parsing SGML was a pain so they created XML to make things better. XML uses SGML. For example in SGML
you have to start and end tags but in XML you can have closing tags which close automatically (“”).
XHTML was created from XML which was used in HTML 4.0. So for example in SGML derived HTML “
” is not valid but in XHTML it’s valid. You can refer XML DTD as shown in the below code snippet.
In short SGML is the parent of every one. Older HTML utilizes SGML and HTML 4.0 uses XHTML which derived from
XML.
What is HTML 5?
HTML 5 is a new standard for HTML whose main target is to deliver everything without need to any additional
plugins like flash, Silverlight etc. It has everything from animations, videos, rich GUI etc.
HTML5 is cooperation output between World Wide Web Consortium (W3C) and the Web Hypertext Application
Technology Working Group (WHATWG).
In HTML 5 we do not need DTD why?
HTML 5 does not use SGML or XHTML it’s completely a new thing so you do not need to refer DTD. For HTML 5 you
just need to put the below doctype code which makes the browser identify that this is a HTML 5 document.
Below are more details of the HTML 5 elements which form the page structure.
<input list="Country">
<datalist id="Country">
<option value="India">
<option value="Italy">
<option value="Iran">
<option value="Israel">
<option value="Indonesia">
</datalist>
What are the different new form element types in HTML 5?
There are 10 important new form elements introduced in HTML 5:-
1. Color.
2. Date
3. Datetime-local
4. Email
5. Time
6. Url
7. Range
8. Telephone
9. Number
10. Search
If you want to create a HTML text with email validation we can set the type as “email”.
For URL validation set the type as “url” as shown in the below HTML code.
If you want to display textbox with number range you can set type to number.
What is SVG?
SVG stands for scalable vector graphics. It’s a text based graphic language which draws images using text, lines, dots
etc. This makes it lightweight and renders faster.
SVG Canvas
Here’s it’s like draw and remember. In other Canvas is like draw and forget. Once something is drawn you
words any shape drawn by using SVG can be cannot access that pixel and manipulate it.
remembered and manipulated and browser can
render it again.
SVG is good for creating graphics like CAD Canvas is good for draw and forget scenarios like animation and
software’s where once something is drawn the games.
user wants to manipulate it.
This is slow as it needs to remember the co- This is faster as there is no intention of remembering things later.
ordinates for later manipulations.
We can have event handler associated with the Here we cannot associate event handlers with drawing objects as
drawing object. we do not have reference of them.
Resolution independent. Resolution dependent.
<style>
.intro
{
background-color:red;
}
</style>
To specify number of columns we need to us column-count. “webkit” and “moz-column” are needed for chrome and
firefox respectively.
-moz-column-count:3; /* Firefox */
-webkit-column-count:3; /* Safari and Chrome */
column-count:3;
-moz-column-gap:40px; /* Firefox */
-webkit-column-gap:40px; /* Safari and Chrome */
column-gap:20px;
Do you want to draw a line between those columns , if yes how much thick ?
<style>
.magazine
{
-moz-column-count:3; /* Firefox */
-webkit-column-count:3; /* Safari and Chrome */
column-count:3;
-moz-column-gap:40px; /* Firefox */
-webkit-column-gap:40px; /* Safari and Chrome */
column-gap:20px;
<div class="magazine">
</div>
Can you explain CSS box model?
CSS box model is a rectangular space around a HTML element which defines border, padding and margin.
Border: - This defines the maximum area in which the element will be contained. We can make the border visible,
invisible, define height and width etc.
Margin: - This defines the spacing between border and any neighboring elements.
.specialtext
{
text-shadow: 5px 5px 5px #FF0000;
}
<style>
.breakword
{word-wrap:break-word;}
</style>
Let’s say the above for loop code is executed on a HTML button click. Now this method execution is synchronous. In
other words the complete browser will wait until the for loop completes. This can further lead to browser getting
freezed and unresponsive with an error message as shown in the screen below.
So if we can move this heavy for loop in a JavaScript file and run it asynchronously that means the browser does
need to wait for the loop then we can have a more responsive browser. That’s what web worker are for.
To create a worker thread we need to pass the JavaScript file name and create the worker object.
To send message to the worker object we need to use “PostMessage” , below is the code for the same.
worker.postMessage();
When the worker thread sends data we get it in the “OnMessage” event on the callers end.
The heavy loop is in the “MyHeavyProcess.js” javascriptfile , below is the code for the same.When the JavaScript file
wants to send message he uses “postmessage” and any message sent from the caller is received in the “onmessage”
event.
var x =0
self.onmessage = function (e) {
for (i = 0; i < 1000000000; i++)
{
x = i + x;
}
self.postMessage(x);
};
Now to implement this kind of requirement developers normally write some kind of PULL code which goes to the
server and fetches data in certain interval. Now PULL solution is good but it makes the network chatty with lot of
calls and also it adds load on the server.
So rather than PULL it would be great if we can have some kind of PUSH solution. In simple words when the server
has updates it will send updates to the browser client. That can be achieved by using “SERVER SENT EVENTS”.
So the first thing the browser needs to do is connect to the server source which will send updates. Let’s say we have
page “stock.aspx” which sends stock updates. So to connect to the page we need to use attach to the event source
object as shown in the below code.
We also need to attach the function where we will receive messages when server sends update. For than we need to
attach function to the “onmessage” event as shown in the below code.
Now from the server side we need to send events. Below are some lists of important events with command that
needs to be sent from the server side.
Event Command
Send data to the client. data : hello
Tell client to retry in 10 seconds retry : 10000
Raise a specific event with data event : successdata : You are logged in.
So for example if we want to send data below is the ASP.NET code for the same. Please note the content type is set
to text/event.
Response.ContentType="text/event-stream";
Response.Expires=-1;
Response.Write("data: " + DateTime.Now.ToString());
Response.Flush();
Response.Write("retry: 10000");
If you want to attach an event we need to use the “addEventListener” event as shown in the below code.
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
From the server side the below message will trigger the “message” function of javascript.
event: message
data : hello
Modern browsers have storage called as “Local storage” in which you can store this information.
localStorage.setItem(“Key001”,”India”);
To retrieve data from local storage we need to use “getItem” providing the key name.
You can also store JavaScript object’s in the local storage using the below code.
var country = {};
country.name = “India”;
country.code = “I001”;
localStorage.setItem(“I001”, country);
var country1 = localStorage.getItem(“I001”);
If you want to store in JSON format you can use “JSON.stringify” function as shown in the below code.
localStorage.setItem(“I001”,JSON.stringify(country));
To create a session storage you need to use “sessionStorage.variablename” . In the below code we have a created a
variable called as “clickcount”.
If you refresh the browser the count increases. But if you close the browser and start again the “clickcount” variable
starts from zero.
if(sessionStorage.clickcount)
{
sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
}
else
{
sessionStorage.clickcount = 0;
}
What is difference between session storage and local storage?
Local storage data persists forever but session storage is valid until the browser is open, as soon as the browser
closes the session variable resets.
What is WebSQL?
WebSQL is a structured relational database at the client browser side. It’s a local RDBMS inside the browser on which
you can fire SQL queries.
To execute SQL we then need to use “transaction” function and call “executeSql” function to fire SQL.
db.transaction(function (tx)
{
tx.executeSql('CREATE TABLE IF NOT EXISTS tblCust(id unique, customername)');
tx.executeSql('INSERT INTO tblcust (id, customername) VALUES(1, "shiv")');
tx.executeSql('INSERT INTO tblcust (id, customername) VALUES (2, "raju")');
}
In case you are firing “select” query you will get data is “results” collection which we can loop and display in the
HTML UI.
db.transaction(function (tx)
{
tx.executeSql('SELECT * FROM tblcust', [], function (tx, results) {
for (i = 0; i < len; i++)
{
msg = "<p><b>" + results.rows.item(i).log + "</b></p>";
document.querySelector('#customer).innerHTML += msg;
}
}, null);
});
What is application cache in HTML5?
One of the most demanded things by end user is offline browsing. In other words if internet connection is not
available page should come from browser cache i.e. offline and application cache helps you to achieve the same.
Application cache helps you to specify which files should be cached and not cached.
What are some of the major new API’s that come standard with HTML5?
o To name a few: Media API, Text Track API, Application Cache API, User Interaction, Data Transfer API,
Command API, Constraint Validation API, and the History API.
The canvas element is used to draw graphics images on a web page by using javascript like below
Like below we can add video in html5
1. <video width=“320″ height=“240″ controls=“controls”>
2. <source src=“mysong.mp4″ type=“video/mp4″ />
3. <source src=“mysong.ogg” type=“video/ogg” />
4. </video>
<hgroup>
<h1>Hello</h1>
<h2>How r u?</h2>
</hgroup>