Html uses script tag to include java script coding. The user can include the scripting in any part of html. It can be in <head> tag part , <body> tag part or as a external file.
Here, three
different examples for message displaying are given.
àDisplaying a simple message
- · Create a file in notepad and name it
as “Message.html”
- · Next, Type the following code.
<!DOCTYPE html>
<html>
<head> <title> Simple message</Title>
</head>
<body>
<script language="javascript" >
document.write("Have a pleasant Day")
</script>
</body>
</html>
- · Open the file in your web browser.
- · It shows the output.
è àDisplay birthday message using command button
Here, the java script includes an event handler.
- Now, open new file in a notepad editor.
- Create the program using below code.
<!DOCTYPE html>
<html>
<head>
<script language="javascript" >
<!--
function
birthdayWishes() {
alert("Many
more happy returns of the day")
}
//-->
</script>
</head>
<body>
<!--
document.write("Many more happy returns of the day")
//-->
</script>
< input type = "button" onclick =
"birthdayWishes()" value = "birthdayWishes" />
</body>
</html>
- Next, open your file “birthdayWishes.html” in your web browser.
è Scripting from External java script file
The above two examples use java script coding internally within the file.
When you include the java script as a external file, here is the example.
Steps are given below.
· Create 2 text files. Name them “source.html”
and “sample.js” where “source.html” is the html file and “sample.js” is the external
java script file.
· In “source.html” ,add the below code.
<html>
<head>
<script type = "javascript"
src = "c:\raji\sample.js" ></script>
</head>
<body>
This is the source program
<p onclick="sayHi()">Hi
world</p>
</body>
</html>
· In “ sample.js” file, include the java
script code.
function sayHi() {
alert("Hi world")
}
· The output is given below.
These are some basic java script programs. Try it by yourself.
No comments:
Post a Comment