Saturday, April 30, 2011

JavaScript Syntax - III


What does JavaScript syntax mean? JavaScript syntax refers to a set of rules that determine how the language will be written (by the programmer) and interpreted (by the browser).
The JavaScript syntax is loosely based on the Java syntax. Java is a full blown programming environment and JavaScript could be seen as a sub-set of the Java syntax. Having said this, that is where the similarities end - Java and JavaScript are two totally different things.
In learning JavaScript you will become familiar with terms such as variablesfunctionsstatementsoperatorsdata typesobjectsetc.
It will take most of this tutorial to show you the complete JavaScript syntax. For now, I'll give you a quick intro by showing you an example and explanation.

Example code


<script type="text/javascript">
<!--
 document.write("JavaScript is not Java");
-->
</script>

This results in:
JavaScript is not Java
The above example is how you write text to a web page using JavaScript.

Explanation of code

  • The <script> tags tell the browser to expect a script in between them. You specify the language using the type attribute. The most popular scripting language on the web is JavaScript.
  • The bits that look like HTML comments tag (<-- -->) are just that - HTML comment tags. These are optional but recommended. They tell browsers that don't support JavaScript (or with JavaScript disabled) to ignore the code in between. This prevents the code from being written out to your website users.
  • The part that writes the actual text is only 1 line (document.write("JavaScript is not Java");). This is how you write text to a web page in JavaScript. This is an example of using a JavaScript function (also known as method).

Where to put your scripts?

You can place your scripts in any of the following locations:
  • Between the HTML document's head tags.
  • Within the HTML document's body (i.e. between the body tags).
  • In an external file (and link to it from your HTML document).

No comments:

Post a Comment