Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views7 pages

Week 3 Codes

The document contains multiple HTML code examples demonstrating various JavaScript methods, including alert, prompt, confirm, and functions. Each code snippet illustrates how to use these methods for user interaction and function definitions. The examples cover topics such as function scope and returning values from functions.

Uploaded by

thembi malinga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Week 3 Codes

The document contains multiple HTML code examples demonstrating various JavaScript methods, including alert, prompt, confirm, and functions. Each code snippet illustrates how to use these methods for user interaction and function definitions. The examples cover topics such as function scope and returning values from functions.

Uploaded by

thembi malinga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CODE 1: ALERT METHOD

<html>

<head><title>Alert Method</title></head>

<body bgcolor="blue" text="yellow">

<b>Testing the alert method</b><br />

<h2>

<script type="text/javascript">

document.write("It's a bird, ");

document.write("It's a plane,<br />");

alert("It's Superman!");

</script>

</h2>

</body>

</html>

CODE 2: ALERT2 METHOD

<html>

<head>

<title>Using JavaScript alert box</title>

<script type="text/javascript">

var message1="Match your Quotes and ";

var message2="Beware of Little Bugs ";

alert("Welcome to\nJavaScript Programming!");

alert(message1 + message2);

</script

</head>

</html>
CODE 3: PROMPT METHOD

<html>

<head>

<title>Using the JavaScript prompt box</title>

</head>

<body>

<script type = "text/javascript">

var name=prompt("What is your name?", "");

alert("Welcome to my world! " + name);

var age=prompt("Tell me your age.", "Your age: ");

if ( age == null){ // If user clicks the Cancel button

alert("Not sharing your age with me");

else{

alert(age + " is young");

alert(prompt("Where do you live? ", ""));

</script>

</body>

</html>

CODE 4: CONFIRM METHOD

<html>

<head>

<title>Using the JavaScript confirm box</title>

</head>

<body>

<script type="text/javascript">

if(confirm("Are you really OK?") == true){


alert("Then we can proceed!");

else{

alert("We'll try when you feel better? ");

</script>

</body>

</html>

CODE 5: FUNCTIONS 1

<html>

<head><title>A Simple Function</title>

<script type="text/javascript">

function welcome(){ // Function defined within <head> tags

var place="Durban";

alert("Welcome to "+ place + "!");

</script>

</head>

<body bgcolor="lightblue">

<big>

<div align="center">

<script type="text/javascript">

welcome();

</script>

<b>Durban</b><br />

<img src="Durban.jpg" width="400" height="300">

</div>

</big>

</body>
</html>

CODE 6: FUNCTIONS 2

<html>

<head><title>Passing Arguments</title>

<script type="text/javascript">

function greetings(name){

alert("Greetings to you, " + name);

</script>

</head>

<body background="Ishan.jpg">

<script type="text/javascript">

greetings("Ishan!"); // Passing an argument

</script>

</body>

</html>

CODE 7: FUNCTIONS 3

<html>

<head><title>Functions</title>

<script type="text/javascript">

function hello(){ // Function defined within <head> tags

document.bgColor="lightblue";

alert("Hello to you!");

</script>

</head>

<body bgcolor="silver">
<div align="center">

<a href="JavaScript:hello()"><big>Click here for Salutations</big>

</a><br />

</div>

</body>

</html>

CODE 8: FUNCTIONS 4

<html>

<head><title>Functions and Events</title>

<script type="text/javascript">

function hello(person){ // Function definition

document.bgColor="lavender";

alert("Hey there! " + person);

</script>

</head>

<body>

<div align="center">

<form>

<input type="button"

value="Greet"

onClick="hello('Rocky');"

/>

</form>

</div>

</body>

</html>
CODE 9: FUNCTION SCOPE

<html>

<head><title>Function Scope</title>

<script type="text/javascript">

var name="Ben"; // Global variable

var hometown="Dallas";

function greet(){

var name="Ethan"; // Local variable

var hometown="Seattle";

alert("In function the name is " + name +

" and hometown is "+ hometown);

</script>

</head>

<body>

<script type="text/javascript">

greet(); // Function call

alert("Out of function, name is "+ name +

" and hometown is " + hometown);

</script>

</body>

</html>

CODE 10: FUNCTION RETURN

<html>

<head><title>Return Value</title>

<script type="text/javascript">

function distance(kms, fuel){

return kms/fuel; // Return the result of the division


}

</script>

</head>

<body bgcolor="green">

<div align="center">

<script type="text/javascript">

var dist=eval(prompt("How many kilometres did you drive? ", ""));

var amount=eval(prompt("How much fuel did you use?", ""));

var rate = distance(dist, amount);

// Return value assigned to rate

alert("Your mileage "+ rate +" kms per gallon.\n");

</script>

</div>

</body>

</html>

You might also like