0% found this document useful (0 votes)
24 views16 pages

Javacsript Introduction

Uploaded by

sohamghaware654
Copyright
© © All Rights Reserved
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)
24 views16 pages

Javacsript Introduction

Uploaded by

sohamghaware654
Copyright
© © All Rights Reserved
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/ 16

UNIT 2

Chap 1 :JavaScript
-Ms. Pratiksha harwalkar
What is JavaScript?
JavaScript was initially created to “make web pages
alive”.
The programs in this language are called scripts. They
can be written right in a web page’s HTML and run
automatically as the page loads.
Scripts are provided and executed as plain text. They
don’t need special preparation or compilation to run.
In this aspect, JavaScript is very different from
another language called Java.
JavaScript is used in web technologies to make web
pages interactive. Along with HTML and CSS,
JavaScript is widely used in web servers and
databases. Hence it has wide popularity among these
kinds of websites.
JavaScript is also being used widely in game
development and Mobile application development.
A Simple JavaScript Program
You should place all your JavaScript code
within <script> tags (<script> and </script>) if you are
keeping your JavaScript code within the HTML
document itself.
This helps the browser distinguish your JavaScript
code from the rest of the code. As there are other
client-side scripting languages (Example: VBScript), it
is highly recommended that we specify the scripting
language you use.
We have to use the type attribute within the <script>
tag and set its value to text/javascript like this:
<script type="text/javascript“ src=“filename.js”
language=“javascript”>
Hello World Example: save file as filename.html
<html>
<head>
<title>My First JavaScript code!!!</title>
<script type="text/javascript">
alert(“Popup Box!");
document.write(“Print Command”);
</script>
</head>
<body>
</body>
</html>
OUTPUT:
Hello World!

Note: type="text/javascript" is not necessary in HTML5.


Using JavaScript in an HTML Document

JavaScript example is easy to code. JavaScript


provides 3 places to put the JavaScript code:
1. within body tag, ---- internal js
2. within head tag and ------ internal js
3. external JavaScript file. (.html + .js)
NOTE:
• Javascript extension - filename.js
• Link .js .html with the help of src (source) attribute
in <script> tag
• Syntax : <script src=“name of the js with extension”>
1. Within <body> tag
<html>
<body>
<script type="text/javascript">
document.write("JavaScript is a simple
language for all learners");
</script>
</body>
</html>

OUTPUT:
JavaScript is a simple language for all learners
The script tag specifies that we are using JavaScript.
The text/javascript is the content type that provides
information to the browser about the data.
The document.write() function is used to display
dynamic content through JavaScript. We will learn
about document object in detail later.
2. Code between the head tag
Let’s see the same example of displaying alert dialog
box of JavaScript that is contained inside the head
tag.
In this example, we are creating a function msg(). To
create function in JavaScript, we need to write
function with function_name as given below.
function function_name()
To call function, we need to work on event. Here we
are using onclick event to call msg() function.
2. Within <head > tag
<!DOCTYPE html>
<html>
<head>
<script>
document.write("Script within head tag");
</script>
</head>
<body>
<h1>Content of Body tag</h1>
</body>
</html>
OUTPUT:
Script within head tag
Content of the Body tag
3. External JavaScript file
We can create external JavaScript file and embed it in
many html page.
It provides code re usability because single JavaScript
file can be used in several html pages.
An external JavaScript file must be saved by .js
extension. It is recommended to embed all JavaScript
files into a single file. It increases the speed of the
webpage.
Let’s create an external JavaScript file that prints Hello
World in a alert dialog box.
message.js
function msg(){
alert("Hello Javatpoint");
}

Let’s include the JavaScript file into html page. It calls


the JavaScript function on button click.
index.html
<html>
<head>
<script type="text/javascript" src="message.js">
</script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click"
onclick="msg()"/>
</form>
</body>
OUTPUT:

You might also like