Programming Tutorials

Running JavaScript

By: aathishankaran in Javascript Tutorials on 2007-03-21  

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:

  • Code reuse
  • Readability
  • 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.

Example:

<script>
console.log("Hello World");
</script>

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.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Javascript )

Latest Articles (in Javascript)