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
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments