Coding in Javascript
Coding in 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
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:
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:
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:
Coding In JavaScript
1. Follow Tools-> Internet Options from the menu
Coding In JavaScript
1. Follow Tools-> Options
Coding In JavaScript
1. Follow Tools-> Preferences
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:
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:
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:
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.
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 :
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