Creating Dynamic Web Page UNIT III
Creating Dynamic Web Page UNIT III
NIT III
• new Date()
• new Date(milliseconds)
• new Date(Date string)
• new Date(year, month, day, hours, minutes, seconds, milliseconds)
JavaScript Date Methods
• Method Description
• now() Returns the numeric value corresponding to the current
time
• getFullYear() Gets the year according to local time
• getMonth() Gets the month, from 0 to 11 according to local time
• getDate() Gets the day of the month (1–31) according to local time
• getDay() Gets the day of the week (0-6) according to local time
• getHours() Gets the hour from 0 to 23 according to local time
• getMinutes Gets the minute from 0 to 59 according to local time
• setFullYear() Sets the full year according to local time
• setMonth() Sets the month according to local time
• setDate() Sets the day of the month according to local time
E x a m p l es
• < !D OCT YPE h tml >
• <html>
• <body>
•
<h2>JavaScript Dates</h2>
•
<p>setDate() sets the day of the month of a date:</p>
•
<p id="demo"></p>
•
<script>
• const d = new Date();
• d.setDate(15);
• document.getElementById("demo").innerHTML = d;
• </script>
•
</body>
• </html>
•
2. Set date
<!DOCTYPE html>
<html><body><h2>JavaScript Dates</h2>
<p>setDate() sets the day of the month of a date:</p>
<p id="demo"></p>
<script>
const d = new Date("July 21, 1983 01:15:00");
d.setDate(15);
d.setFullYear(2000);
document.getElementById("demo").innerHTML = d;
</script></body></html>
Current time example
• <html>
• <body>
• Current Time: <span id="txt"></span>
• <script>
• var today=new Date();
• var h=today.getHours();
• var m=today.getMinutes();
• var s=today.getSeconds();
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
• </script>
• </body>
• </html>
What will be the output?
• By array literal
• By creating instance of Array directly (using new keyword)
• By using an Array constructor (using new keyword)
•
• 1) JavaScript array literal []
•The syntax of creating array using array literal is given below
var a1 = [,,,];
var a2 = new Array(3);
0 in a1
0 in a2
a) true false
b) false true
c) true true
d) false true
Using Arrays- sample Questions
• Q2. What will be returned if you look for the index of something
that does not exist?
b) Client side
d) User side
5. What is the purpose of the area element?
a) C++
b) JavaScript
c) C
d) JAVA
• To include the HTTP server in the node module, what function
do we use?
• A) get()
• B) Require()
• C) createServer()
• D)None of these
• We can kill a process in Node.js using the keyboard shortcut?
• Ctrl+ C
• Ctrl+ B
• Ctrl+ K
•What is the full form of npm?
a) Hello,js
b) node Hello.js
c) javascript Hello.js
d) node Hello
is a debugging tool for NodeJS?
a) Node Inspector
b) REPL
c) Node Console
a) npm –version
b) node –version
c) npm getVersion
a) True
b) False
c) Cannot be determined
c)Both A and B
a) Chrome V8
b) Microsoft Chakra
c) SpiderMonkey
d) Node En
All APIs of Node.JS are.
A - Asynchronous
B - Synchronous
C - Both of the above.
D - None of the above.
Which of the following methods is used to access HTML elements using Javascript?
a) getElementbyId()
b) getElementsByClassName()
c) Both A and B
d) None of the above
Which of the following methods can be used to display data in some
form using Javascript?
a) document.write()
b) console.log()
c) window.alert()
d) All of the above
What will be the output of the following code snippet?
<script type="text/javascript"> a
= 5 + "9";
document.write(a); </script>
Compilation Error
14
Runtime Error
59
5. What will be the output of the
following JavaScript code?
<p id="demo"></p>
<script>
var js = 10;
js *= 5;
document.getElementById("demo").innerHTML = js;
</script>
• a) 10
• b) 50
• c) 5
• d) Error
8. What will be the output of the following
JavaScript code snippet?
• // JavaScript Equalto Operators
• function equalto()
• {
• int num=10;
• if(num==="10")
• return true;
• else
• return false;
• }
• a) false
• b) true
• c) compilation error
• d) runtime error
External JavaScript file
• DOCTYPE html>
• <html>
• <body >
• <h1>Generating Quotes</h1>
• <script>
• function thoughts_authors() {
• quotes[0] = "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.";
• quotes[3] = "Be the change that you wish to see in the world.";
• }
• </script>
• </body>
• </html>
changing the background color through a
random number in js
• <!DOCTYPE html>
• <html>
• <head>
• <meta charset="utf-8">
• </head>
• <body>
• <script>function random_bg_color() {
• document.body.style.background = bgColor;
• }
•
• random_bg_color();
• </script>
• </body>
• </html>
•
IMAGE OBJECT
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>Document</title>
• <style>
• body {
• }
• .result {
• font-size: 18px;
• font-weight: 500;
• color: rebeccapurple;
• }
• </style>
• </head>
• <body>
• <script>
• newImage.src = "https://i.picsum.photos/id/195/536/354.jpg";
• BtnEle.addEventListener("click", () => {