Information Technology IT – 21 Programming for the web | e-Consult
21 Programming for the web (1 questions)
JavaScript can be included in an HTML document in three different ways:
- Inline JavaScript: JavaScript code is embedded directly within an HTML element using the
onclick,onmouseover, etc., attributes.Advantages: Simple for small snippets of code. Disadvantages: Poor code organization, difficult to maintain, increases HTML file size, and can negatively impact browser performance due to parsing overhead.
- Internal JavaScript: JavaScript code is placed within a
tag inside theorsection of the HTML document.Advantages: Better code organization than inline, all JavaScript code is in one file. Disadvantages: Can still increase HTML file size, and the code is not easily reusable across multiple pages.
- External JavaScript: JavaScript code is stored in a separate .js file and linked to the HTML document using the
tag.Advantages: Best code organization, promotes reusability, reduces HTML file size, and improves browser performance as the browser can cache the .js file. Disadvantages: Requires an extra HTTP request to load the .js file.
Generally, external JavaScript is the preferred approach due to its advantages in code organization, maintainability, and performance.