Lab 4 314
Lab 4 314
pg. 1
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
JAVA
SCRIPT
JavaScript is a scripting or programming language that allows
(JS)
you to implement complex features on web pages — every time
a web page does more than just sit there and display static
information for you to look at — displaying timely content
updates, interactive maps, animated 2D/3D graphics, scrolling
JavaScript Where To
1- You can either include the 2- You can keep the JavaScript
JavaScript code internally code in a separate external
within your HTML file and then point to that file
document itself from your HTML document.
pg. 2
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
Task 1 😊
1.create HTML page with the following JavaScript Popup
Boxes:
a: prompt, ask the user to enter the name
b: alert
what is the difference?
Prompt: method returns the input value if the user clicks "OK", otherwise it
returns null
Alert: method is used when you want information to come through to the user.
pg. 3
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
pg. 4
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
Code:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
h1 {
color: rgb(221, 28, 28);
background-color:rgb(153, 202, 172);
border: 2px solid;
padding: 10px;
text-align: center;
font-family: Georgia, Times, 'Times New Roman', serif;
}
p {
text-align: center;
font-family: Georgia, Times, 'Times New Roman', serif;
color: blue;
}
form {
text-align: center;
font-family: Georgia, Times, 'Times New Roman', serif;
}
.button1 {
display: flex;
justify-content: center;
align-items: center;;
font-family: Georgia, Times, 'Times New Roman', serif;
}
</style>
<script>
function prompt1() {
var Pro =prompt("Enter your name: ");
if (Pro != null) {
document.getElementById("pdb").innerHTML= "Hi "+ Pro +" how are you today? ";
}
}
function call(name) {
alert("Have a nice day "+ name +" :) ");
}
</script>
pg. 5
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
</head>
<body style="background-color: rgb(186, 219, 120);">
</body>
</html>
pg. 6
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
2.create HTML page with input form that read the username
and button that has event (onclick) which display hi massage
with the username.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<style type="text/css">
h3 {
color: rgb(221, 28, 28);
text-align: center;
font-family: Georgia, Times, 'Times New Roman', serif;
}
form {
text-align: center;
font-family: Georgia, Times, 'Times New Roman', serif;
}
.button1 {
display: flex;
justify-content: center;
align-items: center;;
font-family: Georgia, Times, 'Times New Roman', serif;
}
pg. 7
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
</style>
<script>
function call(name){
document.write("Hi "+name)
}
</script>
</head>
<body style="background-color: rgb(154, 247, 174)">
<h3> User Name </h3>
<form>
<label for="first name "> </label>
<input type="text" name="firname">
<button onclick="call(form.firname.value)"> Displey Masssage </button>
</form>
</body>
</html>
pg. 8
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
Code:
<html>
<head>
<style type="text/css">
p {
width: 150px;
height: 50px;
text-align: center;
border: 2px solid;
padding: 20px;
border-color: rgb(114, 141, 17);
}
div {
border: 2px solid;
padding-left: 30px;
padding-top: 20px;
width:20cm;
height: 7cm;
}
h3 {
font-family:'Times New Roman', Times, serif;
color:rgb(131, 83, 21);
}
pg. 9
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
</style>
<script>
function change0(){
var color = document.getElementById("color").value;
document.getElementById("BGC").style.backgroundColor=color;
}
</script>
</head>
<body style="background-color:rgb(166, 245, 189)">
<div>
<h3> Dear user one of the features of this website is you can change the
background color </h3>
<p id="BGC"> Please choose a background color </p>
<select onchange="change0()" id="color">
<option value="white"> None </option>
<option value="Red"> Red :) </option>
<option value="Green"> Green :) </option>
<option value="Blue"> Blue :) </option>
</div>
</section>
</body>
</html>
pg. 10
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
4.create JS code with While loop statement
5.create JS code with For loop statement
pg. 11
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
Code:
<html>
<head>
<style type="text/css">
p {
color: rgba(148, 4, 88, 0.692);
border: 2px solid;
border-color:rgb(194, 191, 6);
width: 5cm;
text-align: center;
}
</style>
</head>
<body style="background-color:rgb(206, 250, 235)">
<p> While Loop </p>
</body>
</html>
<script>
var num =10;
while(num >= 0){
document.write("Number: "+ num +"<br>");
num--;
}
document.write("<p> For Loop </p>"+"<br>");
for(i=0 ;i<11; i++){
document.write("number "+ i +"<br>")
}
</script>
pg. 12
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
6.create JS code with IF, Else Statement
pg. 13
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
Code:
<html>
<head>
<style type="text/css">
div {
border: 2px solid;
padding-left: 20px;
border-color:#2320ecbe;
width: 15cm;
height: 4cm;
}
h2,h3 {
font-family:'Courier New', Courier, monospace;
}
h2 {
color: rgb(23, 4, 128);
}
h3 {
color:rgba(20, 103, 228, 0.747)
}
</style>
</head>
<body style="background-color: rgb(235, 163, 163)">
<div>
<h2> create JS code with IF, Else Statement </h2>
<h3> what color is the sky? </h3>
<input type="radio" name="color" value="blue"
onclick="color(this.value)">blue
<input type="radio" name="color" value="green"
onclick="color(this.value)">green
<input type="radio" name="color" value="red"
onclick="color(this.value)">red
</div>
</body>
</html>
<script>
function color(rainbow){
if (rainbow =="blue"){
window.alert("Right :)");
}
else {
window.alert("wrong :(")
}
}
</script>
pg. 14
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
7- Document Object Model:
7.1: Display Date on <p> when the user click on the button
7.2: insert DOM
Code:
<html>
<head>
</head>
<body>
<h2 style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;">
insert DOM </h2>
<button onclick="insert()"> insert </button>
</body>
</html>
<script>
function insert(){
var NewEl =document.createElement("p");
var text =document.createTextNode("PHP is not easy at all...");
NewEl.appendChild(text);
document.body.appendChild(NewEl);}
</script>
pg. 15
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
7.3 replace DOM
Code:
<html>
<body>
<div id="main">
<p id="demo"> old paragraph </p>
</div>
<button onclick="replace()"> replace </button>
</body>
</html>
<script>
function replace() {
var Newel = document.createElement("p");
var text = document.createTextNode("new p");
Newel.appendChild(text);
var oldel = document.getElementById("demo");
var par = document.getElementById("main");
par.replaceChild(Newel,oldel)
}
</script>
pg. 16
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
7.4 Remove DOM
Code:
<html>
<head>
<style type="text/css">
button {
background-color:rgb(138, 245, 138);
}
body {
background-color:rgb(248, 212, 165);
}
p,li {
color: rgb(69, 69, 197);
}
</style>
</head>
<body>
<div id="main">
pg. 17
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
<p id="demo">this is a paragraph</p>
</div>
<button onclick="remove()">remove paragraph</button>
<ul id="list">
<li>Coffee</li>
<li>Water</li>
<li>Juice</li>
</ul>
<button onclick="remove2()">remove from list</button>
</body>
</html>
<script>
function remove(){
var part=document.getElementById("main");
var child=document.getElementById("demo");
part.removeChild(child);
}
function remove2(){
var par=document.getElementById("list");
par.removeChild(par.firstElementChild);
}
</script>
pg. 18
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
7.5 DOM animation
Code:
<html>
<style>
#main {
height: 400px;
width: 500px;
position: relative;
background-color: rgb(216, 11, 189);
}
#animate {
height: 50px;
pg. 19
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
width: 50px;
position: absolute;
background-color: rgb(67, 147, 238);
}
</style>
<body>
<h2> DOM Animation </h2>
<p> <button onclick="animate1()"> Click me </button> </p>
<div id="main">
<div id="animate"></div>
</div>
<script>
function animate1(){
var elementMO =document.getElementById("animate");
var pos = 0;
var id=setInterval(frame , 5);
function frame(){
if(pos==350){
clearInterval(id);
}
else{
pos++;
elementMO.style.top=pos + "px"
elementMO.style.left=pos +"px"
}
}
}
</script>
</body>
</html>
pg. 20
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
8. JavaScript Form validation
pg. 21
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
Code:
<html>
<head>
<style>
p,h2 {
font-family:'Times New Roman', Times, serif;
font-weight: bold;
color: rgb(13, 126, 73);
}
body {
background-color:rgba(129, 207, 243, 0.322);
margin: 20px;
border: 2px solid;
padding: 15px;
padding-right: 200px;
}
input {
border: 2px solid;
border-radius: 5px;
border-color: rgb(9, 170, 63);
}
.erorr {
color: red;
font-weight: 15;
}
</style>
</head>
<body>
<h2> Welcome please complete the following </h2>
<form name="ErrorForm" method="post" action="" onsubmit="return valid()">
<p> User Name: </p>
<input type="text" name="Name">
<p class="erorr" id="name erorr"></p>
<p> Password: </p>
<input type="text" name="passw">
<p class="erorr" id="pass erorr"></p>
<p> Email: </p>
<input type="text" name="email">
<p class="erorr" id="emali erorr"></p>
<script>
function valid(){
var name =document.ErrorForm.Name.value;
var pass =document.ErrorForm.passw.value;
var email =document.ErrorForm.email.value;
var course=document.ErrorForm.cour.value;
if(name== ""){
document.getElementById("name erorr").innerHTML=" Enter a name! ";
return false;
}
var regx = /^[a-zA-Z][a-zA-Z0-9]+$/ ;
if(regx.test(name)==false)
{
document.getElementById("name erorr").innerHTML=
"Enter a vaild name"+"<br>"+ " 1-you have to start with a letter"+"<br>"+"2-
your name must contains numbers";
return false;
}
else{
document.getElementById("name erorr").innerHTML="valid";
}
if(pass==""){
document.getElementById("pass erorr").innerHTML="enter pass :(";
return false;}
var regxp=/^[A-Z][a-zA-Z0-9]+$/;
if(regxp.test(pass)==false){
document.getElementById("pass erorr").innerHTML="enter valid pass :("+
"<br>"+"* Start with a capetal letter"+"<br>"+"* password must contain numbers";
return false; }
else{
document.getElementById("pass erorr").innerHTML="valid";
}
if(email==""){
document.getElementById("emali erorr").innerHTML=" Enter an Email :() ";
return false;
}
pg. 23
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
var regxpr = /^[a-zA-Z0-9]+@[a-zA-Z0-9.-]+$/;
if(regxpr.test(email)==false){
document.getElementById("emali erorr").innerHTML=
"Enter a vaild email! "+
"<br>"+"1- Start with a letter or number"+"<br>"+"2- You have to include @
"+"<br>"+"3- Then the orgnastion name"+"<br>"+"example: [email protected]";
return false;
}
else{
document.getElementById("emali erorr").innerHTML="valid";
}
}
</script>
pg. 24
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
Task 2 😊
The Goal of this Task: Demonstrate knowledge of basic
JavaScript through building one-page website with self-
updating time (a clock) using JavaScript.
Code:
<!DOCTYPE html>
<html>
<head>
<title> Clock Using JavaScript </title>
<style>
body {
font-family: 'Times New Roman', Times, serif;
background-color: rgb(253, 197, 197);
}
.wrap {
text-align: center;
padding: 200px 0 0;
}
h1 {
font-size: 50px;
pg. 25
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
margin-bottom: 15px;
}
#clock span {
display: inline-block;
box-shadow: 0 5px 15px #1b42ce;
width: 100px;
background-color: rgb(121, 114, 211);
height: 100px;
line-height: 100px;
}
#clock span:first-child{
margin-left: 0;
}
#clock span:last-child{
background-color: #fa256c;
}
</style>
</head>
<body>
<div class="wrap">
<h1 id="greetings"></h1>
<h1 id="clock"></h1>
</div>
<script src="javascriptCLOCK.js"> </script>
</body>
</html>
function clock(){
var date = new Date();
var hours=date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var midday;
hours = updateTime(hours);
minutes = updateTime(minutes);
seconds = updateTime(seconds);
var name = "Raghed";
midday = (hours>=12) ? " PM" : " AM";
document.getElementById("clock").innerHTML =
"<span>"+hours+"</span>" + ":" + "<span>"+minutes+"</span>" + ":" +"<span>"+
seconds+"</span>" + midday;
var time = setTimeout(function(){
pg. 26
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
clock();
},1000);
if(hours <12 ){
var greeting ="Good Moring " + name;
}
if (hours >= 12 && hours <=18){
var greeting = "Good Afternoor " + name ;
}
if (hours>= 18 && hours <= 24){
var greeting = "Good Evening " +name;
}
document.getElementById("greetings").innerHTML = greeting;
}
function updateTime(k){
if (k <10 ){
return "0" +k }
else{
return k;
}
}
clock();
Tags pg. 27
and
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
<html> </html> : these are outer tags that apply to the entire HTML
doc. The first <html> tag indicates the markup language used for the
doc, and the </html> marks the end of the web page.
<head> </head> : the <head> tag contains the me metadata that is
required to describe the styles, fonts, linked files, page title, and scripts
used by the HTML doc.
<title> </title> : this tag contains the text appears as the title of a doc
in the browser. There cannot be more than one <title> element in an
HTML doc.
<body> </body> : the <body> tag contains all the text, data, and
images that are displayed when the HTML doc is opened in a browser.
<h> </h> : the <h1/h2/h3/h4/h5/h6> tags indicate that the text is a
header. <h1> is usually used for the title of the page, while the others are
used to style smaller headings on the doc.
<p> </p> : this tag contains the text that should appear as a paragraph
on screen. The browser starts a new line and adds margins for spacing
around the paragraph.
pg. 28
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
pg. 30
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
Padding: property defines a padding (space) between the text and the
border.
pg. 31
Raghed Alrefi CS314
4112248 Ms. Basimah Aljhne
References
(n.d.). Retrieved from https://www.javatpoint.com/html-tags
Chris, K. (2021, August 24). What is HTML – Definition and Meaning of Hypertext Markup Language. Retrieved from
FreeCodeCamp: https://www.freecodecamp.org/news/what-is-html-definition-and-meaning/
How to Design Digital Clock using JavaScript ? (2021, Jul 30). Retrieved from GeeksforGeek's:
https://www.geeksforgeeks.org/how-to-design-digital-clock-using-javascript/
Malek, P. (2020, October 29).   and HTML Space Challenges and Tricks. Retrieved from mailtrap:
https://mailtrap.io/blog/nbsp/
Reactor, H. (2021, August 26). What is JavaScript used for? Retrieved from Hack Reactor:
https://www.hackreactor.com/blog/what-is-javascript-used-for
pg. 32