Thursday, August 20, 2015

JavaScript

Reduce Activity in Loops

Loops are often used in programming.
Each statement in a loop, including the for statement, is executed for each iteration of the loop.
Search for statements or assignments that can be placed outside the loop.

Bad Code:

for (i = 0; i < arr.length; i++) {

Better Code:

l = arr.length;
for (i = 0; i < l; i++) {
The bad code accesses the length property of an array each time the loop is iterated.
The better code accesses the length property outside the loop, and makes the loop run faster.

Reduce DOM Access

Accessing the HTML DOM is very slow, compared to other JavaScript statements.
If you expect to access a DOM element several times, access it once, and use it as a local variable:

Example

obj = document.getElementById("demo");
obj.innerHTML = "Hello";

Try it Yourself »

Reduce DOM Size

Keep the number of elements in the HTML DOM small.
This will always improve page loading, and speed up rendering (page display), especially on smaller devices.
Every attempt to search the DOM (like getElementsByTagName) will benefit from a smaller DOM.

Avoid Unnecessary Variables

Don't create new variables if you don't plan to save values.
Often you can replace code like this:
var fullName = firstName + " " + lastName;
document.getElementById("demo").innerHTML = fullName;
With this:
document.getElementById("demo").innerHTML = firstName + " " + lastName

Delay JavaScript Loading

Putting your scripts at the bottom of the page body, lets the browser load the page first.
While a script is downloading, the browser will not start any other downloads. In addition all parsing and rendering activity might be blocked.
NoteThe HTTP specification defines that browsers should not download more than two components in parallel.
An alternative is to use defer="true" in the script tag. The defer attribute specifies that the script should be executed after the page has finished parsing, but it only works for external scripts.
If possible, you can add your script to the page by code, after the page has loaded:

Example

<script>
window.onload = downScripts;

function downScripts() {
    var element = document.createElement("script");
    element.src = "myScript.js";
    document.body.appendChild(element);
}
</script>

Future of the Internet

In this closing segment, we are going to look at the future of the Internet, but through the eyes of what we have learnt about its past.

Now predicting the future is a dangerous thing to do! As we have seen, the history of the Internet is filled with the wrecks of prophets who thought they knew what would happen. But we are armed with a lot more information. By knowing the past, we can more happily predict the future. So, with a bit of risk, here goes.

Earlier on we talked about digital convergence, and that's definitely on the future agenda still. One interesting development in this area is called ENUM - which is a new standard that allows every telephone number to become a world wide web address. So one day, in the not too distant future, we will have worked out easy ways to send instant messages from mobile phones to computers and back again. Messaging, and particularly instant messaging, something of a new genre for the Internet - are here to stay, and are only going to get better.

The other thing which will grow through ENUM and related developments is what they call voice over ip - or internet phones. Already we are seeing these being adopted both by large corporations in internal networks and by hobbyists in networks such as SKYPE - this isn't going to go away either, because at this point of time it offers very significant savings as compared with old fashioned telephony costs. It will take a while because telecommunications companies aren't exactly nimble, but one day the convergence of Internet and telephone futures will arrive.

Talking of mobile phones, wireless and mobility are again trends we can expect to see more of in tomorrow's Internet. We are already seeing the growth of wireless hotspots for mobile travellers in airports, hotels and other places, and of course we are seeing a growing range of mobile devices. We have talked about "anywhere, anytime" access for a long while - we can certainly expect to see that grow.

Another thing we can expect to see is a lot of new developments in what is called the 'peer to peer' space. If you know what Napster was, you can see what peer to peer is. Peer to peer is unlike a traditional network with a central computer through which all traffic passes - peer to peer allows almost direct communication on the network with any other computer, for tasks such as trading music and files. Napster spread like wildfire across the Internet, and since then we have seen many other similar developments. Applications like this will continue. 

But there are a couple of broader issues out there as well. One of these is multilingual domain names, which we learnt about in a previous section. As 80% of the people on this planet don't speak English as their first language, there is a natural desire to be able to use their own language on the Internet. Now that's difficult at present, because the core of the Internet finds all those difficult foreign characters hard to handle. But it's unlikely this issue will go away. It may involve some significant changes to the Internet, but most people believe that will happen.

Some of the things people would like to control are illegal software, music piracy, and pornography. They're bound to continue, as are worms, viruses and spam. Now worms and viruses simply exploit weaknesses in the Internet to be able to proliferate freely. 

Remembering again that the Internet was built for another purpose altogether, and has been patched up like a quilt over the intervening years, its inevitable that some work will be done to give the Internet greater security and stop the spread of fraud. Similarly, those spam messages that use fraud (they pretend to be someone else sending the message) can be fixed. I suspect that it's only a matter of time before we have a more trustworthy and secure Internet.

Another thing that will become apparent as this all happens is the necessity for access for all people in all countries, at affordable rates. Once a medium goes so far with penetration of usage, it starts to become an economic necessity - and at the same time, a human right to have access. That's starting to happen with the Internet, and growth won't go away for a long time.

All this means that our future Internet, rather than having 600 million users, may have close to 6 billion. So we are about 10% of the way there, and there is a lot of growth to come.

What does that say for the protocols and for governance? Well, the only thing that is for sure is that things will continue to change. We can expect to see a larger role for the United Nations - we can expect some major protocol changes - but, to the end user, all of this should be able to happen without much fuss or concern.

HTML 5

What is New in HTML5?

The DOCTYPE declaration for HTML5 is very simple:
<!DOCTYPE html>
The character encoding (charset) declaration is also very simple:
<meta charset="UTF-8">

HTML5 Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>

<body>
Content of the document......
</body>

</html>
NoteThe default character encoding in HTML5 is UTF-8.

New HTML5 Elements

The most interesting new elements are: 
New semantic elements like <header>, <footer>, <article>, and <section>.
New form control attributes like number, date, time, calendar, and range.
New graphic elements: <svg> and <canvas>.
New multimedia elements: <audio> and <video>.
NoteIn the chapter HTML5 Support, you will learn how to "teach" old browsers to handle HTML5 semantic.

New HTML5 API's (Application Programming Interfaces)

The most interesting new API's are:
  • HTML Geolocation
  • HTML Drag and Drop
  • HTML Local Storage
  • HTML Application Cache
  • HTML Web Workers
  • HTML SSE
NoteLocal storage is a powerful replacement for cookies.

Elements Removed in HTML5

The following HTML4 elements have been removed from HTML5:
ElementUse instead
<acronym><abbr>
<applet><object>
<basefont>CSS
<big>CSS
<center>CSS
<dir><ul>
<font>CSS
<frame>
<frameset>
<noframes>
<strike>CSS
<tt>CSS
NoteIn the chapter HTML5 Migration, you will learn how to easily migrate from HTML4 to HTML5. 

HTML History

Since the early days of the web, there have been many versions of HTML:
VersionYear
Tim Berners-Lee invented www1989
Tim Berners-Lee invented HTML1991
Dave Raggett drafted HTML+1993
HTML Working Group defined HTML 2.01995
W3C Recommended HTML 3.21997
W3C Recommended HTML 4.011999
W3C Recommended XHTML 1.02000
HTML5 WHATWG First Public Draft2008
HTML5 WHATWG Living Standard2012
HTML5 W3C Final Recommendation2014
Tim Berners-Lee invented the "World Wide Web" in 1989, and the Internet took off in the 1990s.
From 1991 to 1998, HTML developed from version 1 to version 4. 
In 2000, the World Wide Web Consortium (W3C) recommended XHTML 1.0. 
The XHTML syntax was strict, and the developers were forced to write valid and "well-formed" code.
In 2004, WHATWG (Web Hypertext Application Technology Working Group) was formed in response to slow W3C development, and W3C's decision to close down the development of HTML, in favor of XHTML.
WHATWG wanted to develop HTML, consistent with how the web was used, while being backward compatible with older versions of HTML.
In the period 2004-2006, the WHATWG initiative gained support by the major browser vendors.
In 2006, W3C announced that they would support WHATWG.
In 2008, the first HTML5 public draft was released.
In 2012, WHATWG and W3C decided on a separation:
WHATWG will develop HTML as a "Living Standard".
A living standard is never fully complete, but always updated and improved. New features can be added, but old functionality can not be removed.
The WHATWG Living Standard was published in 2012, and is continuously updated.
W3C will develop a definitive HTML5 and XHTML5 standard, as a "snapshot" of WHATWG.
The W3C HTML5 recommendation was released 28 October 2014.

Founder of HTML

  1. Tim Berners-Lee
    HTML, Inventor
  2. Sir Timothy John "Tim" Berners-Lee, OM, KBE, FRS, FREng, FRSA, DFBCS, also known as TimBL, is an English computer scientist, best known as the inventor of the World Wide Web. Wikipedia 
  3. BornJune 8, 1955 (age 60), London, United Kingdom
  4. NationalityBritish
  5. SpouseRosemary Leith (m. 2014)

Wednesday, August 19, 2015

HTML

Playing a YouTube Video in HTML

To play your video on a web page, do the following:
  • Upload the video to YouTube
  • Take a note of the video id
  • Define an <iframe> element in your web page
  • Let the src attribute point to the video URL
  • Use the width and height attributes to specify the dimension of the player
  • Add any other parameters to the URL

Example - Using iFrame (the recommended method)

<iframe width="420" height="315"
src=
"http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1">
</iframe>