Javacsript Introduction
Javacsript Introduction
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!
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");
}