Java Script - A New Look


Published on

Published in: Education
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java Script - A New Look

  1. 1. JavaScript – A New Look<br />Mr. Geek<br />
  2. 2. Interpreted language what makes a static webpage dynamic.<br />Is not complied but interpreted by the browser.<br />Browser downloads the code and run it.<br />Manipulates HTML object also known as DOM, such as form items, anchors.<br />What is JavaScript?<br />
  3. 3. Developed by Brendan Eich of Netscape, originally under the name mocha.<br />Became livescript before renaming into JavaScript.<br />Unrelated to Java, despite the name.<br />It was developed to create a dynamic website.<br />AJAX has given a brand new look to JavaScript; Now JavaScript has become more important than ever.<br />History of JavaScript<br />
  4. 4. Different browsers implements different versions of JavaScript.<br />Some browsers like Internet Explorer even add their own functions to enhance the code.<br />Every JavaScript codes does not work in every browser; therefore code should be written with the user demographic in mind.<br />Newer browsers are adopting standard JavaScript; also known as “Class A” browsers.<br />Browser Issues<br />
  5. 5. To make static page more interactive by adding dynamic functions.<br />Validate form before submitting.<br />Since the data can be validated or cleaned up before sending to the server, JavaScript helps lessen the burden on the server.<br />JavaScript can dynamically create and manipulate DOM objects, hence HTML pages can be created and changed programmatically without refreshing the page.<br />JavaScript can fetch HTML objects as needed, so that unnecessary data does not transfer – this makes the page/data load faster.<br />Why use JavaScript?<br />
  6. 6. JavaScript can be implemented in three basic styles.<br />Embed in Body of the document.<br />Embed in Header of the document.<br />Load from an external (.js) file.<br />Embed in Body of the document<br />This is the easiest way to implement JavaScript.<br />Embed where needed using <script>…</script>.<br />This is very poor way of implementation as the functions cannot be easily reused.<br />Script is not available for the part of the document which is above the implantation.<br />Difficult to manage codes.<br />How to use JavaScript?<br />
  7. 7. Embed in Header of the document<br />Here <script>…</script> is implemented before the <body> tag.<br />This ensures that JavaScript functions are available for all the parts of the documents (DOMs).<br />Cannot be applied across multiple documents (webpages).<br />Changes have to be replicated across multiple documents manually.<br />Does not make the JavaScript code independent.<br />More codes. <br />How to use JavaScript?<br />
  8. 8. Load from an external (.js) file<br />Most efficient way of implementing JavaScript Codes.<br />Multiple documents can reference single JavaScript files using <script src=“JSPATH”></script>.<br />Changes in the script file with automatically reflected to all the documents that references it.<br />This enables true separation of header components.<br />One weakness of this implementation is that a page has to load all the JavaScript codes regardless it uses it or not. This is helped by caching; but what if the js file becomes multiple megabytes in size (this has become true in newer AJAX-enabled website).<br />One way to overcome this weakness is that breaking up the file into multiple files and load dynamically only when a function is needed. AJAX makes this possible.<br />How to use JavaScript?<br />
  9. 9. With the explosion of AJAX enabled components like JavaScript trees and grids, JS files tend to become very large – often in multiple megabytes.<br />Large files makes the web application slow due to net transfer and browser processing of large files.<br />An ideal solution will be only download only the part of the file, whose function is called.<br />This can be accomplished by creating multiple files with relevant piece of the code. <br />When the function that resides in a file is called; dynamically download the JS file with that function and load it to the browser – after it loads to the browser then only execute the function.<br />All this process should happen within few seconds.<br />Dynamic Load of JavaScript File<br />
  10. 10. Previously JavaScript was restricted to form validation and interaction with HTML objects.<br />Now AJAX (which is basically a JavaScript implementation) enables us to do more than just validation.<br />It is used to fetch data without refreshing the page.<br />It is used to fetch only the required data and modify only the part of the webpage.<br />If you have a high traffic website, JavaScript is a must for form validation so that it is less burden for the server.<br />When to Use JavaScript?<br />
  11. 11. Since all the codes of JavaScript is available to the public; it is advisable not to put confidential business logic in the JavaScript code.<br />As JavaScript runs in client machine you cannot use it to interact with the files and databases in the server. You would need an intermediary language like ASP.Net, PHP and CGI to do the server functions.<br />If you need some of your data to be indexed by Search Engines; it is advisable to fetch data in static manner, instead of using AJAX. Till date Search Engines are not very AJAX friendly.<br />When NOT to use JavaScript?<br />
  12. 12. JavaScript follows basic rules of programming; and the codes are similar to c++ and Java.<br />Every statement should end in “;” (Although many browsers do not enforce it).<br />Variable<br />variables can be public or private. <br />Variables are case sensitive.<br />All the variables that are defined outside a function are public.<br />Variables that are defined inside a function but with out a “var” keyword is also a public variable.<br />Since JavaScript code is read and interpreted top to bottom; variables defined at the bottom are not available to the top.<br />Eg: varfirstName=“santosh”;<br />Anatomy of JavaScript - Variable<br />
  13. 13. Array<br />Array helps store and manipulate multiple values at multiple location in the memory.<br />You can sort through an array.<br />Array always have index number; can also have keyword.<br />Array index starts with zero.<br />Array can be single or multiple dimension.<br />Eg: var animals = new Array(“Tiger”, “Fish”, “Snake”);<br />document.write(animals[0]) prints Tiger.<br />Anatomy of JavaScript - Array<br />
  14. 14. Functions<br />Functions in JavaScript can be used in two ways:<br />As a regular function.<br />As a signature of a class. This means any function in JavaScript can instantiate an object with new keyword. This is UNLIKE any other programming language. This is a very powerful feature, at the same time can make things very confusing.<br />Just like in Class you can extend property of any function by using prototype keyword. This makes inheritance possible in JavaScript – which makes JavaScript truly object oriented programming.<br />Functions can be nested; meaning you can define a function within a function.<br />A JavaScript functions can be used as: function, method, constructor, class and modules<br />Anatomy of JavaScript - Functions<br />
  15. 15. JavaScript is an event driven language.<br />Initiation of any class and method is done by a user clicking (or other interaction) any part of the document or a program initiating an event.<br />Each object can listen to various types of events.<br />Once an event is fired other functions work in harmony to accomplish relevant task.<br />Example: <input type=button onClick=“doSomething();”><br />Here a button is capturing a click event. Once it captures, a function called doSomething is executed.<br />Anatomy of JavaScript - Events<br />
  16. 16. Example of a function:<br />function Call(number){phone_dial(number)}<br />Example of pseudo function:<br />Call: function(number){phone_dial(number)}<br />Extending a Class:<br />Call.prototype.hangup(){phone_onhook();}<br />Now the Call class has a hangup function.<br />Instantiating an object from function:<br />var c = new Call();<br />c.hangup(); //this is valid since we extened Call using prototype.<br />Nesting functions:<br />Call: function(number){Home: function(){dial:function(){} }}<br />Not it is possible to call the nested function dial.var c=new Call();c.Home.dial();<br />Anatomy of JavaScript - Functions<br />
  17. 17. JavaScript and HTML’s implementation got out of control as new browsers did not follow the standard.<br />A need for Standard implementation arose; which gave birth to XHTML, which follow the strict guidelines as in XML. <br />This give rise to Document Object Modal (DOM) which treats every element and nodes in the XHTML as an object.<br />Like in XML you can manipulate XHTML via programming languages like JavaScript. You can reference to any node using XPATH.<br />One can even use XQUERY to get and manipulate one or collection of similar nodes.<br />This gave documents more structure and flexibility of manipulate.<br />Since JavaScript was already doing some manipulation; it was an ideal choice of language for DOM Scripting.<br />CSS plays a vital role in formatting the DOM objects. JavaScript can manipulate CSS as well. It can apply CSS formatting to one or multiple objects in the DOM.<br />Example:<br />var elements = document.getElementsByTagName("body")[0].childNodes;for(i=0;i<elements.length;i++){ if(elements[i].nodeType == 1 && elements[i].id) alert(elements[i].id);}<br />DOM Scripting<br />
  18. 18. Invention of AJAX has revolutionize the way JavaScript is implemented.<br />Even though basic AJAX means fetching the data asynchronously from the server (without refreshing the whole page), DOM Scripting has become synonymous to AJAX implementation. <br />Combination of DOM Scripting and AJAX has truly made a webpage a rich, dynamic and powerful medium to share information.<br />AJAX has made the webpage very efficient by enabling it to download data in parts when needed and render only part of the webpage which is changed due to the new data.<br />Different browsers have different implementation of AJAX but their basic concept is the same.<br />AJAX tries to make stateless HTTP protocol seem stateful.<br />…More about AJAX in next slideshow<br />Reborn of JavaScript with AJAX<br />