Saturday 2 June 2012

Basics of Javascript1


What is JavaScript

JavaScript is an object-oriented scripting language used to enable programmatic access to computational objects within a host environment. Although also used in other applications, it is primarily used in the form of client-side JavaScript, implemented as part of a web browser, providing enhanced user interfaces and dynamic websites. JavaScript support is built right into web browsers. Provided that the visitors to your site are using web browsers that support JavaScript (most do) and have JavaScript enabled (it is by default) then your JavaScript will run.

Differences of JavaScript and Java

There are many differences between Java and JavaScript language as

JavaScript
Java
Interpreted by client mainly
Compiled on server and changes it in HTML and user gets only HTML coding as in ASP, PHP etc languages. Because user's browser only reads HTML and JavaScript coding.
Object-based language because it has no class or inheritance features
Object-oriented and it has class or inheritance features.
It has no graphics capabilities
It has high graphic capabilities
It can't read or write files
It can read and write files
It has no networking support like can not let us to chat or communicate within network computers using bind(), listed() or send() properties
It has all networking support
History of JS

When World Wide Web created in early 1990s all web pages were static. When we view the web page on internet we exactly saw what the page was set up to show. Netscape was the first to bring out a programming language that would allow web pages to become interactive - they called it LiveScript and it was integrated into the browser. The programming language called Java became very well known and so Netscape decided to cash by renaming the language to JavaScript what they have created for Netscape Navigator browsers. At the time Netscape Navigator was by far the more popular browser and so later versions of Internet Explorer implemented versions of Jscript and vbScript that were more and more like JavaScript. It is very big cause not to support all script commands by all the browsers. Although scripting language was too great to deal its future developments so in 1996 JavaScript was handed over to an international standards body called ECMA who then became responsible for the subsequent development of the language. As a result of this the language was officially renamed ECMAScript or ECMA-262 but most people still refer to it as JavaScript.

Function of JS

JavaScript is a client-side programming language meant for use on the web and when you request for any page on web script coding automatically runs on our computer because our browser have the ability to read JS coding. Client-Side scripting language means which runs on user's computer and Server-Side scripting language means, which runs on server computer from where the web pages are requested.

Where to write the JavaScript Program

JavaScript is an interpreted language and so no special program is required. Any plain text editor such as Notepad is quite satisfactory for being able to write JavaScript. That said, an editor, which colorizes the code to make it easier to see what is what makes it easier to find your mistakes. An editor named Komodo will be nice for to write JavaScript coding.

Difference between HTML and JavaScript

HTML and JavaScript are two completely different things. HTML is a mark-up language designed for defining static web page content. JavaScript is a programming language designed for performing dynamic tasks.

Features of JavaScript

There are lots of features as listed below:

  1. A Great Programming tool for HTML

    Professional Web designers are undoubtedly adept in using HTML because JavaScript is a powerful scripting language that helps HTML designers to effectively and interactively design websites and web pages in a very simple and efficient way.

  1. Handles Dynamic Effects

    JavaScript is such a powerful scripting language, which has features to achieve dynamic effects in web pages. Using the features available in JavaScript, the designer can decide to have dynamically placed text at run time.
     
  2. Browser Detection

    One of the powerful features of JavaScript is its ability to detect client side browser. Browser detection feature of JavaScript helps to achieve independent platforms. JavaScript can detect the type of browser the visitor is using and programmatically switch the page to show customised pages designed for different browsers. Thus by making use of browser detection feature of JavaScript, the designer gets better control over the browser.
     
  3. Time Savings

    JavaScript also has the feature of validating data submitted at the client level. This helps in saving the processing time of the server because JavaScript initially creates the validation on the client side.
     
  4. Interpreted Language

    It is an interpreted language, meaning that it can be used or executed with ease without pre-compilation.
How to use JS in web page

There are mainly two ways to use JS in web page:
  • Using JS commands within the web page with HTML coding
  • Using JS commands from external JS file
Using JS commands within the web page with HTML coding
We use to write all codes in a same page which has the extension .htm, .html, .php, .asp, .aspx, .cfm etc.

<html>
<head>
<title>TESTING JS</title>
<script language="JavaScript" type="text/javascript">
window.print()
</script>
</head>
<body onload="disco()">
    <div> This is the content to be printed. </div>
</body>
</html>


More examples

JavaScript in head

<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
<p>We usually use the head section for functions (to be sure that the functions are loaded before they are called).</p>
</body>
</html>


JavaScript in HTML H1 tag

<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>


JavaScript in body

<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>


Using JS commands from external JS file(s)

To use JavaScript (.js) files in HTML page, we have to write all JavaScript coding in a file, which has exetesion .js, and remember not use opening and closing <SCRIPT> tags in .js files. Assume that we have myjava.js file which has some coding, without only <SCRIPT> tags. And now to use it in HTML file write the following code in HTML file

HTML file content

<html>
<head>
<script type="text/javascript" src="external.js">
</script>
</head>
<body onload="message()">
This text is from body
</body>
</html>


external.js file content

function message()
{
alert("This alert box was called with the onload event");
}

HAVE A HAPPY CODING!
  

No comments:

Post a Comment