Executing Java Scripts

By: aathishankaran Viewed: 192 times  Printer Friendly Format    


JavaScript execution begins after the HTML document loads into the browser, but before the users can interact with the document. The browser reads in all JavaScript statements, as it does HTML statements, and then begins interpreting the JavaScript code. 

If your JavaScript scripts are stored in a separate file, they are also evaluated when the page loads and before any script actions take place.

 All JavaScript statements that are contained within a function block are interpreted, and execution does not occur until the function is called from a JavaScript event. JavaScript statements that are not within a function block are executed after the document loads into the browser. The execution results of the latter will be apparent to the users when they first view the page. The second process which JavaScript executes statements is though function calls. Any statement contained within a function will not be executed until a JavaScript event calls the function. . 

Writing the Script

As in any other programming language, JavaScript statements can be implemented using various methodologies. A function has to be defined in the <HEAD>section and then calling these functions within the HTML body is the best w to take advantage of the object-based JavaScript language. Java script is simple to understand. Although HTML statements are not case-sensitive, JavaScript statements are.

 When beginning to write your code, keep the following items in mind:

v     Code reuse

v     Readability

v     Ease of modification

You can use JavaScript tags in either the body or the head of a document. Placing the <SCRIPT>in the head rather than the body ensures that all statements will be evaluated (and executed, if necessary) before the user interacts with the document. The hazards of putting script statements in the body of the document are varied. Depending on the specific tags and the order of the document, you can never be positive that the user will not interact with the script in the wrong manner or react to the page before the script has fully loaded or executed. If any of these occur, the effect that you want for your page might not be seen. (After all your effort, who wants that?) The practice of defining your JavaScript functions and then calling them from the body will ensure that all the functions are evaluated before the user can begin interaction with the page. 

Running the Script 

As you probably realized, JavaScript scripts are as simple to load as HTML documents. You do not have to explicitly execute any code to run your scripts, because you place your code in the HTML document or call it explicitly in the first script line, your script will run when the page loads. Remember that not all the code will necessarily execute immediately upon loading. Code that is enclosed in a function call is only evaluated when the page loads but does not execute until the function is explicitly called from a JavaScript event. Code that is not enclosed in a function call runs after the page finishes loading but before the user has a chance to interact with the page.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial