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

Coding in Javascript

The document provides an overview of coding websites using JavaScript. It discusses what JavaScript is, its merits and demerits. It explains that JavaScript allows making web pages interactive by controlling browsers and dynamically creating HTML content. The document also covers enabling JavaScript in browsers like Internet Explorer, Firefox and Opera. It demonstrates basic JavaScript syntax like using script tags and semicolons. It provides an example of using document.write to print "Hello World".

Uploaded by

John Kiarie
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Coding in Javascript

The document provides an overview of coding websites using JavaScript. It discusses what JavaScript is, its merits and demerits. It explains that JavaScript allows making web pages interactive by controlling browsers and dynamically creating HTML content. The document also covers enabling JavaScript in browsers like Internet Explorer, Firefox and Opera. It demonstrates basic JavaScript syntax like using script tags and semicolons. It provides an example of using document.write to print "Hello World".

Uploaded by

John Kiarie
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

BIT 221:INTERNET PROGRAMMING

Lecturer: Thuku Lawrence


Lecture 4: Coding web sites using JavaScript
OBJECTIVES
1. Recap on what JavaScript is.
2. Merits and demerits of JavaScript

Coding In JavaScript
3. Enabling JavaScript in web browsers
4. JavaScript syntax
5. Placing JavaScript in HTML

2
WHAT IS JAVASCRIPT ?

JavaScript started life as LiveScript, but Netscape changed the name, possibly because
of the excitement being generated by Java.to JavaScript. JavaScript made its first
appearance in Netscape 2.0 in 1995 with a name LiveScript.

Coding In JavaScript
JavaScript is a lightweight, interpreted programming language with object-oriented
capabilities that allows you to build interactivity into otherwise static HTML pages.

The general-purpose core of the language has been embedded in Netscape, Internet
Explorer, and other web browsers
JavaScript is:
1.JavaScript is a lightweight, interpreted programming language

2.Designed for creating network-centric applications

3.Complementary to and integrated with Java

4.Complementary to and integrated with HTML

5.Open and cross-platform 3


CLIENT-SIDE JAVASCRIPT
 :
 Client-side JavaScript is the most common form of the language. The
script should be included in or referenced by an HTML document for the

Coding In JavaScript
code to be interpreted by the browser.
 It means that a web page need no longer be static HTML, but can include
programs that interact with the user, control the browser, and dynamically
create HTML content.
 The JavaScript client-side mechanism features many advantages over
traditional CGI server-side scripts. For example, you might use JavaScript
to check if the user has entered a valid e-mail address in a form field.
 The JavaScript code is executed when the user submits the form, and only
if all the entries are valid they would be submitted to the Web Server.
 JavaScript can be used to trap user-initiated events such as button clicks,
link navigation, and other actions that the user explicitly or implicitly
4
initiates.
ADVANTAGES OF JAVASCRIPT:

The merits of using JavaScript are:


 Less server interaction: You can validate user input before

Coding In JavaScript
sending the page off to the server. This saves server traffic,
which means less load on your server.
 Immediate feedback to the visitors: They don't have to
wait for a page reload to see if they have forgotten to enter
something.
 Increased interactivity: You can create interfaces that react
when the user hovers over them with a mouse or activates
them via the keyboard.
 Richer interfaces: You can use JavaScript to include such
items as drag-and-drop components and sliders to give a 5
Rich Interface to your site visitors.
LIMITATIONS WITH JAVASCRIPT:

 We can not treat JavaScript as a full fledged


programming language. It lacks the following important
features:

Coding In JavaScript
 Client-side JavaScript does not allow the reading or
writing of files. This has been kept for security reason.
 JavaScript can not be used for Networking applications
because there is no such support available.
 JavaScript doesn't have any multithreading or
multiprocess capabilities.

6
Coding In JavaScript
ENABLING JAVASCRIPT IN BROWSERS

7
JAVASCRIPT IN INTERNET EXPLORER:

Here are simple steps to turn on or turn off JavaScript in your


Internet Explorer:

Coding In JavaScript
1. Follow Tools-> Internet Options from the menu

2. Select Security tab from the dialog box

3. Click the Custom Level button

4. Scroll down till you find Scripting option

5. Select Enable radio button under Active scripting

6. Finally click OK and come out

To disable JavaScript support in your Internet Explorer, you


need to select Disable radio button under Active 8
scripting.
JAVASCRIPT IN FIREFOX:

Here are simple steps to turn on or turn off JavaScript in


your Firefox:

Coding In JavaScript
1. Follow Tools-> Options

2. from the menu

3. Select Content option from the dialog box

4. Select Enable JavaScript checkbox

5. Finally click OK and come out

To disable JavaScript support in your Firefox, you should


not select Enable JavaScript checkbox.
9
JAVASCRIPT IN OPERA:

Here are simple steps to turn on or turn off JavaScript in


your Opera:

Coding In JavaScript
1. Follow Tools-> Preferences

2. from the menu

3. Select Advanced option from the dialog box

4. Select Content from the listed items

5. Select Enable JavaScript checkbox

6. Finally click OK and come out

To disable JavaScript support in your Opera, you should


10
not select Enable JavaScript checkbox.
WARNING FOR NON-JAVASCRIPT BROWSERS:

If you have to do something important using JavaScript then you can display a warning message to
the user using <noscript> tags.
You can add a noscript block immediately after the script block as follows:

Coding In JavaScript
<html>
<body>
<script language="javascript" type="text/javascript"> <!-- document.write("Hello World!") //-->
</script>
<noscript>
Sorry...JavaScript is needed to go ahead.
</noscript>
</body>
</html>

 Now, if user's browser does not support JavaScript or JavaScript is not enabled then message from
</noscript> will be displayed on the screen.
11
Coding In JavaScript
12
JAVASCRIPT SYNTAX
SYNTAX
 A JavaScript consists of JavaScript statements that are placed within the
<script>... </script> HTML tags in a web page.
 You can place the <script> tag containing your JavaScript anywhere within you

Coding In JavaScript
web page but it is preferred way to keep it within the <head> tags.
 The <script> tag alert the browser program to begin interpreting all the text
between these tags as a script. So simple syntax of your JavaScript will be as
follows
 <script ...> JavaScript code </script> The script tag takes two important
attributes:
 language: This attribute specifies what scripting language you are using.
Typically, its value will be javascript. Although recent versions of HTML (and
XHTML, its successor) have phased out the use of this attribute.
 type: This attribute is what is now recommended to indicate the scripting
language in use and its value should be set to "text/javascript".
 So your JavaScript segment will look like:
13
 <script language="javascript" type="text/javascript"> JavaScript code </script>
EXAMPLE: YOUR FIRST JAVASCRIPT SCRIPT:

Let us write our class example to print out "Hello World".


<html>

Coding In JavaScript
<body>
<script language="javascript" type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>

14
EXPLANATION
 We call a function document.write which writes a string
into our HTML document. This function can be used to
write text, HTML, or both. So above code will display

Coding In JavaScript
following result:
Hello World!

15
OPTIONAL ELEMENTS
Whitespace and Line Breaks:
 JavaScript ignores spaces, tabs, and newlines that appear in

Coding In JavaScript
JavaScript programs.
 Because you can use spaces, tabs, and newlines freely in your
program so you are free to format and indent your programs in
a neat and consistent way that makes the code easy to read
and understand.
Semicolons are Optional:
 Simple statements in JavaScript are generally followed by a
semicolon character, just as they are in C, C++, and Java.
JavaScript, however, allows you to omit this semicolon if your
statements are each placed on a separate line. For example,
16
the following code could be written without semicolons
EXAMPLE
<script language="javascript“ type="text/javascript"> <!–
var1 = 10

Coding In JavaScript
var2 = 20 //-->
</script>
But when formatted in a single line as follows, the
semicolons are required:
<script language="javascript" type="text/javascript"> <!--
var1 = 10; var2 = 20; //--> </script>
Note: It is a good programming practice to use
semicolons.
17
CASE SENSITIVITY:

 JavaScript is a case-sensitive language. This means that


language keywords, variables, function names, and any
other identifiers must always be typed with a consistent

Coding In JavaScript
capitalization of letters.
 So identifiers Time, TIme and TIME will have different
meanings in JavaScript.
 NOTE: Care should be taken while writing your
variable and function names in JavaScript.

18
COMMENTS IN JAVASCRIPT:

 JavaScript supports both C-style and C++-style


comments, Thus:

Coding In JavaScript
 Any text between a // and the end of a line is treated as a
comment and is ignored by JavaScript.
 Any text between the characters /* and */ is treated as a
comment. This may span multiple lines.
 JavaScript also recognizes the HTML comment opening
sequence <!--. JavaScript treats this as a single-line
comment, just as it does the // comment.
 The HTML comment closing sequence --> is not
recognized by JavaScript so it should be written as //-->.
19
EXAMPLE
<script language="javascript" type="text/javascript">
<!–

Coding In JavaScript
// This is a comment. It is similar to comments in C++ /* *
This is a multiline comment in JavaScript * It is very similar
to comments in C Programming */
//-->
</script>

20
Coding In JavaScript
JAVASCRIPT PLACEMENT IN HTML FILE

21
JAVASCRIPT PLACEMENT
There is a flexibility given to include JavaScript code
anywhere in an HTML document. But there are
following most preferred ways to include JavaScript in

Coding In JavaScript
your HTML file.
1. Script in <head>...</head> section.

2. Script in <body>...</body> section.

3. Script in <body>...</body> and <head>...</head>


sections.
4. Script in and external file and then include in
<head>...</head> section.
22
JAVASCRIPT IN <HEAD>...</HEAD> SECTION:

 If you want to have a script run on some event, such as when a


user clicks somewhere, then you will place that script in the head
as follows:

Coding In JavaScript
<html>
<head>
<script type="text/javascript"> <!--
function sayHello() { alert("Hello World") } //--> </script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</html> 23
What is the output of this code?
JAVASCRIPT IN <BODY>...</BODY> SECTION:

 If you need a script to run as the page loads so that the script generates
content in the page, the script goes in the <body> portion of the
document. In this case you would not have any function defined using

Coding In JavaScript
JavaScript:
<html>
<head>
</head>
<body>
<script type="text/javascript"> <!-- document.write("Hello World") //-->
</script>
<p>This is web page body </p>
</body>
</html>
24
What is the output of this code?
JAVASCRIPT IN <BODY> AND <HEAD> SECTIONS:

You can put your JavaScript code in <head> and <body> section altogether as
follows:
<html>

Coding In JavaScript
<head>
<script type="text/javascript">
<!-- function sayHello() { alert("Hello World") } //--> </script>
</head>
<body> <script type="text/javascript"> <!-- document.write("Hello World")
//-->
</script>
<input type="button" onclick="sayHello()" value="Say Hello" /> </body>
</html>

25
What is the output of this code?
JAVASCRIPT IN EXTERNAL FILE :

Here is an example to show how you can include an external JavaScript


file in your HTML code using script tag and its src attribute:
<html>

Coding In JavaScript
<head>
<script type="text/javascript" src="filename.js" ></script>
</head>
<body>
.......
</body>
</html>

To use JavaScript from an external file source, you need to write your all
JavaScript source code in a simple text file with extension ".js" and then 26
include that file as shown above.
CLASS ASSIGNMENT
 Create a simple website that displays your CV details
using JavaScript

Coding In JavaScript
27
NEXT CLASS
 In our next class we shall look at using control structures
in JavaScript

Coding In JavaScript
28
Coding In JavaScript
29
THE END

You might also like