Structure of JavaScript
JavaScript is a dynamic scripting language that is used to add interactivity to static HTML pages by embedding blocks of code on any part of the web page. Usually, JavaScript is implemented on the client side, that is the browser, to provide enhanced and dynamic interaction with web pages. This is the most significant application and implementation of JavaScript.
-
The Primer
-
All JavaScript code should begin with the tag "<script language= "type/javascript">. Adding this line allows for JavaScript to be validated and thus less errors. In cases in which the browser does not support JavaScript the following lines are added to hide the code:
<script language= "type/javascript">
<!---
document.write ("JavaScript platform initiated!")
//- - ></script>
The above code can be inserted anywhere on a HTML code window to display the text "JavaScript platform initiated!" if the browser supports JavaScript.
Variables
-
These are containers that are used to hold data and that can be referenced anywhere on the code. Variables thus allow for data reuse, simplifying the programming process. Variables can be created and initiated at the same time as shown below:
var MyName = "David";
The above variable is called "MyName" and holds the value "David." I can now refer to that variable anywhere on the code by simply calling "MyName."
-
Loops
-
Looping in JavaScript is implemented when you do not want code to follow a straight, linear fashion. This is one of the flow control techniques that allows for certain blocks of code to be run when certain conditions are met. Loops can be implemented using either a single command or multiple commands. Loops form an important structure of any JavaScript that has repeating script blocks. Loops can either be "for" loops, "while" loops or "dowhile" loops.
Forms
-
After creating forms in your average text editor, you can access and manipulate the text field contents of the forms using JavaScript. The contents of these fields can be displayed in a dialog box as a pop up using JavaScript. Form fields can also be updated dynamically using JavaScript by use of what is called an "OnClick" event handler that specifies the JavaScript code that executes when the user clicks on a link on the form. Form structures thus form an important element that allows for client-side updating of form fields.
Functions
-
These are blocks of code that contain statements executed when a certain event or call is made to the function. These blocks of code are thus not executed when, say, a page loads. They require the user to trigger their execution. They thus form an important code structure within a page.
-
References
Resources
- Photo Credit laptop image by Angie Lingnau from Fotolia.com