Comp 2nd Q Reviewer
Comp 2nd Q Reviewer
1. Inline Script
Pros:
Inline scripts are JavaScript code embedded directly within
● Promotes code organization, maintainability, and
the HTML document, typically within HTML attributes,
reusability.
such as onclick, onload, or href.
● Suitable for complex scripts and projects with
multiple pages.
Example:
● Enhances page load performance since the
<button onclick="myFunction()">Click
me</button> browser can cache external scripts.
Pros: Cons:
● Ideal for small, page-specific functionality and ● Requires additional HTTP requests to load the
event handling. external script, which can affect initial page load
● Quick and simple for adding interactivity to times.
specific elements. THE POPUP BOX
● Good for prototyping and experimenting. A popup box, often referred to as a "popup," is a
graphical user interface (GUI) element that appears
Cons: on top of a web page or application window,
● Mixing HTML and JavaScript can make code less providing information, interactivity, or user prompts.
maintainable for larger projects. Popups are typically used to draw attention to
● Not suitable for complex scripts or code that need specific content, messages, or actions, and they can
to be reused. be seen in various forms on websites and in software
applications.
2. Internal Script
Internal scripts, also known as embedded scripts, are Three Common Types of Popup Boxes:
JavaScript codes included within the HTML document
using the <script> element. These scripts can be placed ● Alert Box: An alert box is a simple popup that
within the <head> or the <body> of the HTML file.
displays a message to the user and typically
includes an "OK" button. It's used to provide
information or alerts.
Example:
<script>
Sample Syntax:
// Your JavaScript code goes here
</script>
alert("This is an alert box!");
Pros:
● Prompt Box: A prompt box is a popup that
● Suitable for small to moderately complex scripts.
prompts the user to input data. It contains an
● Can access and manipulate the DOM. input field for the user to enter information
● Easy for beginners and quick scripting needs. and "OK" and "Cancel" buttons.
- `<h1>`: This is an HTML header element, creating a ● functionName: This is the name of the function.
large and bold heading text. You choose a descriptive name that reflects what
the function does. Function names must follow
- `align="center"`: This attribute is used to horizontally variable naming rules in JavaScript, such as no
center-align the text within the `<h1>` element. spaces, starting with a letter, and using camelCase
or underscores for multi-word names.
- `onmouseover="document.bgColor='Green'"`: This is
an inline event handler that specifies what should happen ● parameters (optional): These are placeholders for
when the mouse pointer is moved over the `<h1>` values that the function expects as input.
element. It sets the background color of the entire Parameters are enclosed in parentheses and
document to green using the `document.bgColor` separated by commas if there are multiple
property. The `document.bgColor` property allows you to parameters. For example,
change the background color of the entire webpage. functionName(parameter1, parameter2).
5. Text inside the `<h1>` element: This is the content of the ● return statement (optional): If the function should
heading. In this case, it reads "Change the Background produce a result or return a value, you can use the
Color." return statement. This statement is followed by
the value or expression you want to return. If the
So, when you view this webpage in a browser, the heading function doesn't return a value, you can omit the
text "Change the Background Color" is centered on the return statement.
page. When you move the mouse pointer over it, the
background color of the entire page changes to green. Here's an example of a simple JavaScript function that
When you move the mouse pointer away from the text, adds two numbers and returns the result:
the background color changes back to yellow. The effect is
achieved by using the `onmouseover` and `onmouseout` function addNumbers(a, b) {
event handlers to modify the `document.bgColor` var sum = a + b;
property of the document. return sum;
}
JAVASCRIPT FUNCTIONS
JavaScript functions are blocks of reusable code that can In this example:
be executed to perform a specific task. Functions are a
fundamental concept in JavaScript and other programming ● addNumbers is the function name.
languages. They help you organize your code into smaller, ● a and b are parameters.
manageable pieces and promote code reusability. ● var sum = a + b; is the function code that
calculates the sum of a and b.
A JavaScript function is defined using the function keyword ● return sum; is the return statement that returns
and can take parameters (inputs) and return a value the result.
(output). Functions can be called (invoked) whenever you
need to execute the code they contain. You can call this function with values, like addNumbers(5,
3), to perform the addition and get the result.
The general form of a JavaScript function is as follows:
function functionName(parameters) { JavaScript functions can be more complex and perform
// Function code or logic here various tasks, and they are a fundamental building block
// ... for organizing and reusing code in JavaScript applications.
return result; // Optional
}
4. <body>: This element represents the content of the
webpage.
Scope:
var: Variables declared with var are
function-scoped, meaning they are only
visible within the function where they are
declared. If declared outside any function, HTML Structure:
they are globally scoped.
Head Section:
let and const: Variables declared with let
and const are block-scoped. This means
● The head section contains the title tag that sets } else if (user>=12 && user<=16){
the title of the HTML page to "Variables and alert("Junior High School");
Operators." } else if (user>=17 && user<=18){
alert("Senior High School");
}else{
Script Section (Inside Head): alert("Invalid");
● JavaScript code is embedded in the head section. }
● An array month is declared to store month names. }