Javascript Basics Tutorial 2
- Administrator
- Belajar Javascript,
- 1093 Times Viewed
- 0 Comment
JavaScript Where To or How Referencing Javascript.
Prior to this matter, I hope you've followed the previous material. If you have not read this material, please go back to the early material first. So you will not be at levels that are too steep.
→ Tutorial Javascript Basics for Beginners
→ Javascript Basics Tutorial 1
JavaScript can be placed in the body and head section of an HTML page. script tags in HTML, JavaScript code must be inserted between script open tag and script close tag.
Example:
The older examples may use different types of attributes: script type = "text / javascript".
This type attribute is not required. JavaScript is a scripting language default in HTML. JavaScript Functions and Events A JavaScript function is a block of JavaScript code, which can be run when "asked" to.
For example, the function can be executed when an event occurs, such as when the user clicks the button. You will learn more about the functions and events in the next chapter. JavaScript in the head or body You can put a number of scripts in HTML documents. Scripts can be placed in the body or the head HTML pages, or both. Keeping all the code in one place, always a good habit. JavaScript in the head In this example, the JavaScript function is placed in the head HTML pages. This function is invoked (called) when the button is clicked:
Example:
JavaScript in the body In this example, the JavaScript function is placed in the body section of the HTML page. This function is invoked (called) when the button is clicked (when the button is clicked):
Example:
It is a good idea to put the script under the body element. This can improve page load, because the script compilation can slow the screen. external JavaScript Scripts can also be placed in an external file: myScript.js
External scripts practical when the same code is used in many different web pages. js JavaScript file has the file extension. To use the external script, put the script file name in the src (source) attribute of the script tag:
Example:
You can place an external script reference in the head or body tags as you want. The script will behave as though it is located exactly where the script tag is located. The external script can not contain the script tag.
Advantages external JavaScript Placing JavaScripts in external files have several advantages:
- Separating the HTML and code.
- It makes HTML and JavaScript easier to read and maintain.
- JavaScript file cache can speed up page load