3QL3
3QL3
Third Quarter
Lesson 3
Group Task
Faith
Fairness
Family
Fidelity
JavaScript Syntax Rules
• When JavaScript was introduced, there was a need
for an interface to allow the scrip to access
elements on the page.
• During this infancy, each browser had their own
specification on how to access page elements.
This became a problem for programmers since
there was not set standard to follow and there were
inconsistencies between webpages depending on
the internet browser they are using.
JavaScript Syntax Rules and Variables
• The W3C (World Wide Web Consortium) was
created so that there would be compatibility and
agreement in the adoption of new standards.
• Document Object Model (DOM) is an interface that
allows you to access and manipulate the contents
of a document (in our lessons, a webpage) with
programming language.
JavaScript Syntax Rules and Variables
• DOM is a structured object-oriented representation
of the individual elements of a page with methods,
properties and events of those objects.
• With DOM, programmers can easily create dynamic
content for webpages.
• DOM is not exclusive for JavaScript – other
programming languages such as Java, C++ and C#
also adhere to this standard.
JavaScript Objects, Methods,
Properties and Events
Let’s begin our first JavaScript Program with the
classic “Hello World” display. By now, you already
know how to display “Hello World” using HTML. But
did you know that JavaScript can be used to display it
too? Let us check the code snippet:
1 <html>
2 <head><title>My First JS</title>
3 <script type=“text/Javascript”>
4 document.write (“Hello World”);
5 </script>
6 </head>
7 <body>
8 <br/>
9 This portion holds contents of the body tag.
10 </body>
11 </html>
Let us study the structure of the code snippet:
• Here we placed the JavaScript within the <head>
tags of HTML. This means that JavaScript is
executed even before the <body> tags.
• From lines 3-5 you will see the <script> tags that tell
the browser that the lines of code within those tags
are scripts. After closing the <script> tag, the
browser will proceed with parsing normal HTML
until it encounters another non-HTML script or
instructions.
Parsing - a technique used to analyze and interpret
the syntax of a text or program to extract relevant
information. Essentially, parsing involves breaking
down a complex set of data structures or code into
smaller, more manageable components that can be
analyzed and understood.
• Line 4 holds the actual JavaScript code. The
“document” object refers to the <body> tags of
HTML while “write” is a method of the document
object. This means that you want JavaScript to
write the contents between the parenthesis and
quotes to the HTML document.
JavaScript does not always have to be placed within
the head tags of an HTML file. Below are additional
ways you can place your JavaScript code:
• JavaScript can be placed within HTML elements.
These scripts are normally placed together with
event triggers. Discussion will be done next time.
• You can also create an external file with a *.js
extension to contain JavaScript code that you can
reuse throughout various HTML pages. With an
external file, you can embed it on any HTML file by
placing this code on your HTML.
Syntax: