HTML5 Java Script Interview Questions
HTML5 Java Script Interview Questions
Code Example:
Advantages:
Advantages of using SVG over other image formats (like JPEG and GIF) are:
SVG images can be created and edited with any text editor
SVG images can be searched, indexed, scripted, and compressed
SVG images are scalable
SVG images can be printed with high quality at any resolution
SVG images are zoomable (and the image can be zoomed without degradation)
Differences Between SVG and Canvas:
SVG is XML based, which means that every element is available within the SVG DOM. You
can attach JavaScript event handlers for an element.
In SVG, each drawn shape is remembered as an object. If attributes of an SVG object are
changed, the browser can automatically re-render the shape.
Canvas is rendered pixel by pixel. In canvas, once the graphic is drawn, it is forgotten by the
browser. If its position should be changed, the entire scene needs to be redrawn, including any
objects that might have been covered by the graphic.
Strict : This DTD contains all HTML elements and attributes, but does NOT INCLUDE
presentational or deprecated elements (like font). Framesets are not allowed.
Transitional : This DTD contains all HTML elements and attributes, INCLUDING
presentational and deprecated elements (like font). Framesets are not allowed.
What is HTML5?
Ans : HTML5 is The New HTML Standard with new elements, attributes, and behaviors.
———————————————————————————————-
HTML 5 Features:
Ans :
1. The <canvas> element for 2D drawing
2. The <video> and <audio> elements for media playback
3. local storage support.
4. Added New elements, like <figure>,<small>, <header>, <nav>,<article>, <footer>,
<section>,<mark>
5. New form controls, like placeholder,calendar, date, time, email, url, search,required ,autofocus
6. In HTML5 there is only one <!doctype> declaration: <!DOCTYPE html>
———————————————————————————————-
What is HTML5 Web Storage?
Ans : In HTML5, we can store data locally within the user’s browser.It is possible to store large
amounts of data without affecting the website’s performance.
Web Storage is more secure and faster.
there are two types of Web Storages
1.LocalStorage:stores data locally with no limit
2.SessionStorage:stores data for one session
———————————————————————————————-
How to store data on client in HTML5?
Ans : we can store data using HTML5 Web Storage.
1.LocalStorage
<script type=”text/javascript”>
localStorage.name=”Raj”;
document.write(localStorage.name);
</script>
2.SessionStorage
<script type=”text/javascript”>
sessionStorage.email=”test@gmail.com”;
document.write(sessionStorage.email);
</script>
———————————————————————————————-
How do you play a Video using HTML5?
Example:
<video width=”500″ height=”300″ controls>
<source src=”video1.mp4″ type=”video/mp4″>
</video>
———————————————————————————————-
How do you play a Audio using HTML5?
Example:
<audio controls>
<source src=”audio.mp3″ type=”audio/mpeg”>
</audio>
———————————————————————————————-
Canvas Element in HTML5?
Ans : The canvas element is used to draw graphics images on a web page
———————————————————————————————-
HTML5 <input> Types ?
Ans :
• search
• tel
• time
• color
• email
• month
• date
• datetime
• datetime-local
• number
• range
• url
• week
———————————————————————————————-
HTML5 New Form Attributes?
Ans :
• pattern
• placeholder
• required
• step
• autocomplete
• autofocus
• height and width
• list
• min and max
• multiple
• form
• formaction
• formenctype
• formmethod
• formnovalidate
• formtarget
———————————————————————————————-
What does a <hgroup> tag do?
<hgroup>
<h1>Hello</h1>
<h2>How r u?</h2>
</hgroup>
———————————————————————————————-
Which video formats are used for the video element?
Ans :
Internet Explorer 9+: MP4
Chrome 6+: MP4, WebM, Ogg
Firefox 3.6+ : WebM, Ogg
Safari 5+ : MP4,
Opera 10.6+ : WebM,Ogg
———————————————————————————————-
Difference between HTML4 and HTML5
———————————————————————————————-
What is the <!DOCTYPE> ? Is it necessary to use in HTML5 ?
Ans : The <!DOCTYPE> declaration must be the very first thing in HTML5 document, before
the <html> tag
———————————————————————————————-
What are the New Media Elements in HTML5?
Ans :
• <audio>
• <video>
• <source>
•
• <track>
1 :: What is the status of the development of the HTML 5 standard?
HTML5 is being developed as the next major revision of HTML (HyperText Markup Language),
the core markup language of the World Wide Web. The Web Hypertext Application Technology
Working Group (WHATWG) started work on the specification in June 2004 under the name
Web Applications 1.0. As of March 2010, the specification is in the Draft Standard state at the
WHATWG, and in Working Draft state at the W3C.
2 :: What are the new APIs provided by the HTML 5 standard? Give a brief description of
each
► The canvas element: Canvas consists of a drawable region defined in HTML code with height
and width attributes. JavaScript code may access the area through a full set of drawing functions
similar to other common 2D APIs, thus allowing for dynamically generated graphics. Some
anticipated uses of the canvas include building graphs, animations, games, and image
composition.
► Timed media playback
► Offline storage database
► Document editing
► Drag-and-drop
► Cross-document messaging
► Browser history management
► MIME type and protocol handler registration
6 :: WHAT IS HTML5?
HTML5 is the latest version of HTML standard supporting multimedia and graphical content.
1
<script type=“text/javascript”>
localStorage.lastname=“ZAG”;
document.write(localStorage.lastname);
</script>
SessionStorage – stores data for one session.The data is deleted when the user closes the browser
window.
<script type=“text/javascript”>
sessionStorage.lastname=“ZAG”;
document.write(sessionStorage.lastname);
</script>
11 :: HOW DO YOU PLAY A AUDIO USING HTML5?
We can display audio using the tag as shown below:
<audio controls=“controls”>
<source src=“test.mp3″ type=“audio/mp3″ />
</audio>
12 :: What is the difference between HTMl5 Application cache and regular HTML browser
cache?
HTML5 specification allows browsers to prefetch some or all of a website assets such as HTML
files, images, CSS, JavaScript, and so on, while the client is connected. It is not necessary for the
user to have accessed this content previously, for fetching this content. In other words,
application cache can prefetch pages that have not been visited at all and are thereby unavailable
in the regular browser cache. Prefetching files can speed up the site’s performance, though you
are of course using bandwidth to download those files initially.
<script type=“text/javascript”>
sessionStorage.name=“PHPZAG”;
document.write(sessionStorage.name);
</script>
In HTML5, the data is NOT passed on by every server request, but used ONLY when asked for.
It is possible to store large amounts of data without affecting the website’s performance.and The
data is stored in different areas for different websites, and a website can only access data stored
by itself.
And for creating localstores just need to call localStorage object like below we are storing name
and address
<script type=“text/javascript”>
localStorage.name=“PHPZAG”;
document.write(localStorage.name);
</script>
<script type=“text/javascript”>
localStorage.address=“Newyork USA”;
document.write(localStorage.address);
</script>
17 :: Tell me How to add video and audio in HTML5
The canvas element is used to draw graphics images on a web page by using javascript like
below
<audio controls=“controls”>
<source src=“mysong.ogg” type=“audio/ogg” />
<source src=“mysong.mp3″ type=“audio/mpeg” />
</audio>
19 :: Explain What are the New Media Elements in HTML5? is canvas element used in
HTML5
Below are the New Media Elements have added in HTML5
Tag Description
<audio> For multimedia content, sounds, music or other audio streams
<video> For video content, such as a movie clip or other video streams
<source> For media resources for media elements, defined inside video or audio
elements
For embedded content, such as a plug-in
<track> For text tracks used in mediaplayers
The <!DOCTYPE> declaration must be the very first thing in HTML5 document, before the
<html> tag. As In HTML 4.01, all <! DOCTYPE > declarations require a reference to a
Document Type Definition (DTD), because HTML 4.01 was based on Standard Generalized
Markup Language (SGML). WHERE AS HTML5 is not based on SGML, and therefore does not
require a reference to a Document Type Definition (DTD).
In javascript there's no classes like in Java, what we actually call a class is in reality a function
simulating a class behaviour. For being so flexible, there are many ways to create a class in
javascript, below you'll find 3 ways of doing that.
function Person(name) {
this.name = name;
// Creating an object
person.name; // "Rafael"
It's very important to notice that you have to use the keyword new when creating
a new instance of that class otherwise you will have logical problems regarding the this will
var person = {
name: "",
setName: function(name) {
this.name = name;
person.setName("Rafael");
person.name; // "Rafael"
In this example we don't use a function to define our class, we are creating a singleton object
person with one attribute and one method. You can use that object straightaway, no instantiation
in this case.
That notation is useful when you don't need to create instances of that class or you'll use it just
once in your application.
this.setName = function(name) {
this.name = name;
this.sayHi = function() {
return "Hi, my name is " + this.name;
person.setName("Rafael");
As you can see in the code snippet above, we have a function like the first example and besides
we also have the new keyword before the function declaration. It means that we are creating
one instance of that class at the same time we are declaring it.
The following pattern is the one that I personally prefer and is called 'module pattern', where we
separate our javascript into logical modules, or namespaces. What you'll see below is an example
of how I would separate my user module.
myapplication.usermodule = (function() {
return {
sayHello: function(message) {
return createMessage(message);
})();
myapplication.adminmodule = (function(){
})()
// This is how we call sayHello
myapplication.usermodule.sayHello("This is my module");
Take a look at the previous code and notice how I create my module using the notation below. It
makes the function to be executed immediately because of the parenthesis at the end of the
command. The result of the execution will be an object which will be set to my variable
myapplication.usermodule.
...
myapplication.usermodule = (function() {
})();
So applying this pattern to your code you may have multiple modules and you have the control
over what you want to make public and what to keep private. Besides your code will be more
organized therefore easy to maintain.
This is pretty simple but at the same time some people never came across a triple equals or never
wondered what's the difference.
Double equals == is used to compare the value of two operands:
"2" == 2; // true
2 == 2; // true
Triple equals === is used to compare the value AND type of two operands:
2 === 2; // true
This can be tricky and the best way to keep in your head is to memorise because if you try to
relate javascript null to other languages, it will get more confusing.
In javascript, null is an object with no value and undefined is a type.
var a;
var b = null;
5. Have you already used MVC before? What you like/dislike about it?
As the UI gets more and more complex we need some good ways to keep it more and more
maintainable and reusable, and Some MVC frameworks for javascript have been widely adopted
lately and it's a good plus if you have already used before and knows what's the benefits of them.
The most famous MVC frameworks are backbone.js and angular.js, it's hard to not hear about
them.
There are many advantages in using these frameworks, I can point out some of them:
UI Binding: Some frameworks allow you to do that. So everytime your model changes, the view
reflects it and vice-versa;
Decoupled client: MVC frameworks like backbone.js incentivise you to use REST API's though
their urlRoot attribute in their Models;
function Person(name) {
this.name = name;
Person.prototype.walk = function() {
It's worth mentioning that adding methods via prototype is the most inexpensive way in terms of
performance since the method is tied to the prototype of the class. It means, for every new
instance of class Person, you will have access to the prototype's walk() method. Now, if you
declare walk() method inside the Person class, you will end up recreating the method for every
new instance of Person.
CSS Questions
Float is used when you want to make an element of your page (usually an image) be pushed to
the right or left and make other elements wrap around it.
When you want an element on the left or right of the floating element not to wrap around it, you
can use clear.
Every web project starts with everything neat, all CSS is organized in blocks or different CSS
files and you know where everything is, right?
Right, until your project gets bigger, deadlines get tight, more developers come on board and
someday you notice a strange behaviour in some elements of the page. When you inspect their
styles you spot lots of css overrides coming from everywhere. This is the moment you realise
how messy CSS can be.
Sass is the modern way of doing CSS and can save many lines of code in your stylesheets. This
is possible because Sass works with variables, nested syntax and mathematical operations.
In my opinion one of the nicest features of sass is the possibility to write a selector just once and
put all styles for that inside it. Do you need a more specific selector under an existing one? Just
nest the specifics into the generic one.
Check out the example below taken from their official website. It's awesome how it can "neatify"
your code.
/* .sass */
table.hl
margin: 2em 0
td.ln
text-align: right
li
font:
family: serif
weight: bold
size: 1.2em
/* .css */
table.hl {
margin: 2em 0;
table.hl td.ln {
text-align: right;
li {
font-family: serif;
font-weight: bold;
font-size: 1.2em;
Other questions
Use sprite images whenever possible, try to group small images commonly used in a single file
to be requested just once. See how Google uses sprites in Google Maps to make one request
Javascripts should be at the bottom of the page, instead of in the head as we use to see out
there;
Ensure parallel requests of your JS and CSS files. In order to force the browser to do that, you
can optimize the order you include resources in your page. This item can generate its own blog
post or even a book so I prefer to suggest you a really good reading about it. Check this out, it's
the google's best practices on page speed load.
Browser Caching is also very import to be set for static resources like JS and CSS files, images,
PDFs and HTML. Caching is set in the HTTP header by informing browsers the expiry date or
maximum age. Then browsers can load the last downloaded resource from the cache instead of
request it again.
XHTML is an HTML that follows the XML rules, which means a XHTML document must have
well-formed markups in order to be rendered properly in all web browsers. Differently from
XHTML, the HTML document can be rendered in most of the browsers even with markup errors
such as no closing tags or wrong nested tags.
<head>
<title>This is head</title>
</head>
<BODY>
This is the body of the document with body tag in capital letters
This HTML document above can be opened with no problems in Chrome, even containing many
markup errors because most browsers can fix them for you automatically.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
</body>
</html>
The code above is a well-formed XHTML document and that's the minimum you must have in
order to render it. Notice the declaration of the doctype at the top of the document and the
namespace (xmlns) attribute in html open tag. These elements are mandatory as well as all the
tags in lowercase.
There are several technical differences between these two types of requests, regarding length
limitation, security, caching and a few others. But if someone asks you WHEN would you use it,
I'd say one of the most important points that any front-end developer should take into account is
that we should only use GET for idempotent requests, it means requests that don't make
significant changes in the backend system or database but if you do need to make inserts, updates
or deletes in a database, trigger emails or any other major action, POST is recommended.