PHP Website Development
Home | Contact Us | Sitemap

Custom Search

What is JavaScript?


JavaScript is a scripting language and generally it is used for client-side web development. It is also used to enable scripting access to objects embedded in other applications (for example Microsoft Gadgets in the Windows Sidebar). Despite the name JavaScript, It is not related to the Java programming language. Both Java and JavaScript have the common C syntax, and JavaScript copies many Java names and naming conventions, they are not related. The primary use of JavaScript is to write functions that are embedded in or included from HTML pages and interact with the Document Object Model (DOM) of the page. Examples of JavaScript usage are,

Opening a new window, Popup window, controlling size, position, look of the new window, Validation of web form input values to make sure that they will be accepted before they are submitted to the server. Because JavaScript code can run locally in a user's browser (rather than on a remote server), it can respond to user actions quickly, making an application feel more responsive. Furthermore, JavaScript code can detect user actions, which HTML alone cannot, such as individual keystrokes.

The language was renamed from LiveScript in a co-marketing deal between Netscape and Sun in exchange for Netscape bundling Sun's Java runtime with their then-dominant browser. "JavaScript" is a trademark of Sun Microsystems. It was used under license for technology invented and implemented by Netscape Communications and current entities such as the Mozilla. JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript. JavaScript was first introduced and deployed in the Netscape browser version 2.0B3 in December of 1995.




JavaScript Basic Syntax
 
Javascript is a client-side scripting language. The script is executed by user's browser. Javascript has nothing to do on web server. Basic usages of JavaScript is for client side form fields validation for emptyness, Dropdown Menus, popup windows, alerts, prompts etc.

Example #1 Basic Structure
<script type="text/javascript">
//....Your JavaScript code goes here
</script>

We can repeat above block as many times as required in a single HTML document. The above JavaScript block can be embedded in normal HTML coding provided the JavaScript code must contain starting and ending tags

Example #2 Printing "Hello World!"
<html>
<head>
<title>My First Page With JavaScript </title>
</head>
<body>
<script type="text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>

As seen in the above example to use JavaScript we need to tell first the browser by using a <script> tag. Next we need to set the type of script equal to "text/javascript". In case some browsers do not support Javascript (This can be set from browser's setting), it displays the code in plain text. To overcome this problem we added the comment. The comment was ended with a "//-->" because "//" signifies a comment in Javascript, so we add that to prevent a browser from reading the end of the HTML comment in as a piece of Javascript code.

Notice that there is no semicolon at the end of the statement document.write("Hello World!"). Javascript does not require that you use semicolons to signify the end of each statement, but it is OK if you use, JavaScript will work normally.

The basic for loop syntax is:
for (initialization; condition; incrementor) {
code;
}
The initialization defines the loop variable and its initial value (such as var i=1). The loop will continue to iterate while the conditional expression evaluates as true (like i<5). Each time the loop is executed, the incrementor code is exectued (i++ would increment the loop variable i by 1 each iteration). Putting these together in a simple example,

Example #3 Basic loop structure
<script type="text/javascript">
var  nextline = "<br />";
document.write("For loop code is starting");
document.write(nextline);

for(var i = 0; i < 5; i++){
   document.write("Counter i = " + i");
   document.write(nextline);
}
document.write("For loop code is ended!");
</script>


This code would generate the output,

For loop code is starting
Counter i = 0
Counter i = 1
Counter i = 2
Counter i = 3
Counter i = 4
For loop code is ended!
Our other websites
Free PHP Source Code Download    Free Bollywood News & Downloads   Free Funny SMS Text Messages
 
Home | About Us | Privacy Policy | Site Map | Contact Us

Copyright © 2009-2010 PHPWEBS