Академический Документы
Профессиональный Документы
Культура Документы
JavaScript is the programming language of HTML and the Web.it is one of the 3 languages all web
developers must learn:
It is Client-side scripting used to change interface behaviors within a specific web page, in response
to mouse or keyboard actions or at specified timing events. In this case, the dynamic behavior
occurs within the presentation. Like js , ajax.
1. One of many JavaScript HTML methods is getElementById().:returns the element that has
the ID attribute with the specified value. It returns null if no elements with the specified ID
exists. An ID should be unique within a page. However, if more than one element with the
specified ID exists, the getElementById() method returns the first element in the source
code.
2. (innerHTML): The innerHTML property sets or returns the HTML content (inner HTML) of an
element.
1. Change the elements content using JS, when the page loaded.
<!DOCTYPE html>
<html>
<body>
1
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
<script>
document.getElementById("myP1").innerHTML = "Hello Student.";
document.getElementById("myP2").innerHTML = "How are you?";
</script>
</body></html>
2. Change the elements content using JS, when the button clicked.
This example uses the getElementById() method to "find" an HTML element (with id="demo") and
changes the element content (innerHTML) to "Hello JavaScript"(on button click)
<html><body>
</body></html>
Using functions: If there is more than one change you should write them in a function in a script
which can be written in body or head.
<html>
<body>
<p id="demo">JavaScript can change the style of an HTML element.</p>
2
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
</script>
<button type="button" onclick="myFunction()">Click Me!</button>
<script>
function myFunction() {
document.getElementById("demo").style.fontSize = "25px";
document.getElementById("demo").style.color = "red";
document.getElementById("demo").style.backgroundColor = "yellow";
}</script>
</body></html>
<html> <body>
<button onclick="document.getElementById('myImage').src='pic_bulbon.gif'" id="on">Turn on
the light</button>
<img id="myImage" src="pic_bulboff.gif" style="width:100px">
3
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
<html>
<body>
<script>
function myFunction() {
document.getElementById("myAnchor").innerHTML = "W3Schools";
document.getElementById("myAnchor").href = "https://www.w3schools.com";
document.getElementById("myAnchor").target = "_blank";
</script>
</body>
</html>
<html>
<body>
</body>
</html>
4
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
<body>
<p id="demo"></p>
</body>
</html>
JavaScript Output
1. Using innerHTML
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;</script></body>
</html>
5
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
2. Using document.write().
<!DOCTYPE html>
<html>
<body>
<script>
document.write(5 + 6);
</script>
</body>
</html>
Case 2: if in button Using document.write() after an HTML document is loaded, will delete all existing
HTML.
<!DOCTYPE html>
<html>
<body>
3. Using window.alert()
Example1.
<!DOCTYPE html>
<html>
<body>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
6
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
Example 2:
<html>
<body>
<script>
function myFunction() {
alert(document.getElementById("demo").innerHTML);}</script></body>
</html>
JavaScript Functions
Example1
<html>
<body>
<p id="demo"></p>
<script>
return p1 * p2;
</script>
</body>
</html>
7
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
Function Invocation
The code inside the function will execute when "something" invokes (calls) the function:
Example
<html>
<body>
<p>
</p>
<script>
function light(sw) {
var pic;
if (sw == 0) {
pic = "pic_bulboff.gif"
} else {
pic = "pic_bulbon.gif"
} document.getElementById('myImage').src = pic;
</script>
</body>
</html>
8
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
Example: Calculate the product of two numbers, and return the result:
<p id="demo"></p>
<script>
var x = myFunction(4, 3); // Function is called, return value will end up in x (x=12)
function myFunction(a, b) {
return a * b; // Function returns the product of a and b
}
</script>
Function Return
When JavaScript reaches a return statement, the function will stop executing. Accessing a function
without () will return the function definition instead of the function result.
<p id="demo"></p>
<script>
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
document.getElementById("demo").innerHTML = toCelsius(77);
</script>
Example2
<p id="demo"></p>
<script>
var x = toCelsius(77);
var text = "The temperature is " + x + " Celsius";
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
document.getElementById("demo").innerHTML = toCelsius(77); </script>
Or you can use the function directly, as a variable value:
var text = "The temperature is " + toCelsius(77) + " Celsius";
9
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
</script>
</head>
<body>
</body>
</html>
Example2. Data validation: Check to enter only numbers and between 1-10
<!DOCTYPE html>
10
28/04/2019 Lecture20 Instructor: Asma’a Khtoom
<html>
<body>
<input id="numb">
<p id="demo"></p>
<script>
function myFunction() {
var x, text;
x = document.getElementById("numb").value;
} else {
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
11