0% found this document useful (0 votes)
36 views

Javascript: Marian C. de Luna Instructor

JavaScript is the scripting language of the web. It is used to add interactivity and functionality to web pages. JavaScript can react to events, read and write HTML elements, validate forms, detect browsers, and create cookies. JavaScript code can be embedded directly in HTML pages or linked via external .js files. JavaScript is case sensitive and supports both single-line and multi-line comments to explain code.

Uploaded by

marian_deluna06
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Javascript: Marian C. de Luna Instructor

JavaScript is the scripting language of the web. It is used to add interactivity and functionality to web pages. JavaScript can react to events, read and write HTML elements, validate forms, detect browsers, and create cookies. JavaScript code can be embedded directly in HTML pages or linked via external .js files. JavaScript is case sensitive and supports both single-line and multi-line comments to explain code.

Uploaded by

marian_deluna06
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

JavaScript

Marian C. de Luna
Instructor
JavaScript is THE scripting language of the
Web.
JavaScript is used in millions of Web pages to
add functionality, validate forms, detect
browsers, and much more.
JavaScript is the most popular scripting
language on the internet, and works in all
major browsers, such as Internet Explorer,
Firefox, Chrome, Opera, and Safari.
JavaScript is usually embedded directly into HTML
pages
Everyone can use JavaScript without purchasing a
license
Are Java and JavaScript the same?
NO!
Java and JavaScript are two
completely different languages in both
concept and design!
Java (developed by Sun Microsystems)
is a powerful and much more complex
programming language - in the same
category as C and C++.
What can a JavaScript do?
JavaScript can react to events - A JavaScript can be set to
execute when something happens, like when a page has finished
loading or when a user clicks on an HTML element
JavaScript can read and write HTML elements - A JavaScript
can read and change the content of an HTML element
JavaScript can be used to validate data - A JavaScript can be
used to validate form data before it is submitted to a server. This
saves the server from extra processing
JavaScript can be used to detect the visitor's browser - A
JavaScript can be used to detect the visitor's browser, and -
depending on the browser - load another page specifically designed
for that browser
JavaScript can be used to create cookies - A JavaScript can be
used to store and retrieve information on the visitor's computer
Cookie is a small piece of text stored on a user's computer by a web browser.
Where to Put the JavaScript
JavaScripts in a page will be executed
immediately while the page loads into the
browser. This is not always what we want.
Sometimes we want to execute a script when
a page loads, other times when a user triggers
an event.
JavaScripts in the body section will be
executed WHILE the page loads.
JavaScripts in the head section will be
executed when CALLED.
Scripts in <head>
Scripts to be executed when they are
called, or when an event is triggered,
go in the head section.
If you place a script in the head
section, you will ensure that the script
is loaded before anyone uses it.
Example:
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload
event");
}
</script>
</head>

<body onload="message()">
</body>
</html>
Scripts in <body>
Scripts to be executed when the
page loads go in the body section.
If you place a script in the body
section, it generates the content of a
page.
Example:
<html>
<head>
</head>

<body>
<script type="text/javascript">
document.write("This message is written by
JavaScript");
</script>
</body>

</html>
Scripts in <head> and <body>
You can place an unlimited
number of scripts in your
document, so you can have scripts
in both the body and the head
section.
Example:
<html>
<head>
<script type="text/javascript">
....
</script>
</head>
<body>
<script type="text/javascript">
....
</script>
</body>
Using an External JavaScript
If you want to run the same JavaScript on several
pages, without having to write the same script on
every page, you can write a JavaScript in an
external file.
Save the external JavaScript file with a .js file
extension.
Note: The external script cannot contain the
<script> tag!
To use the external script, point to the .js file in the
"src" attribute of the <script> tag:
Example:
<html>
<head>
<script type="text/javascript"
src=“java.js"></script>
</head>
<body>
</body>
</html>
JavaScript is Case Sensitive
Unlike HTML, JavaScript is case
sensitive - therefore watch your
capitalization closely when you write
JavaScript statements, create or call
variables, objects and functions.
JavaScript Comments
JavaScript comments can be added to
explain the JavaScript, or to make the
code more readable.
Single line comments start with //.
The following example uses single line
comments to explain the code:
<script type="text/javascript">
// Write a heading
document.write("<h1>This is a heading</h1>");
// Write two paragraphs
document.write("<p>This is a
paragraph.</p>");
document.write("<p>This is another
paragraph.</p>");
</script>
JavaScript Multi-Line Comments
Multi line comments start with /* and
end with */.
The following example uses a multi line
comment to explain the code:
<script type="text/javascript">
/*
The code below will write
one heading and two paragraphs
*/
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another
paragraph.</p>");
</script>

You might also like