0% found this document useful (0 votes)
9 views

JS Programs

The document contains code snippets demonstrating different JavaScript functions and methods including: validating textbox input, retrieving the current date, reversing arrays and strings, opening new windows, printing pages, getting the current URL, creating a countdown timer, and displaying the screen height.

Uploaded by

Kanika Shahi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

JS Programs

The document contains code snippets demonstrating different JavaScript functions and methods including: validating textbox input, retrieving the current date, reversing arrays and strings, opening new windows, printing pages, getting the current URL, creating a countdown timer, and displaying the screen height.

Uploaded by

Kanika Shahi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Textbox required validation in JavaScript:

<html>

<body>

<script

type='text/javascript'>

function

textBoxValidation()

if(document.getElementById('txtBox').value.trim()== "")

alert('Textbox should not be

blank');txtBox.focus();

return false;

alert("Your name submitted

sucessfully")return true;

}
</script>

<form>

Name:<input type='text' id='txtBox'/> <input type='button'


onclick="textBoxValidation()"id="btnSubmit"value='Submit' />

</form>
</body>

</html>
Retrieve Today’s Date in JavaScript

<!DOCTYPE html>

<html>

<body>

<p>getDate() method is used for display today's date in JavaScript</p>

<p id="currentDateDate"></p>

<p id="currentMonth"></p>

<p id="currentYear"></p>

<p id="currentFullDate"></p>

<p id="currentTime"></p>

<script>

var date = new Date();

document.getElementById("currentDateDate").innerHTML ="Current Date using


JavaScript:"+date.getDate();

document.getElementById("currentMonth").innerHTML ="Current Month using


JavaScript:"+(date.getMonth()+1);

document.getElementById("currentYear").innerHTML ="Current Year using


JavaScript:"+date.getFullYear();

document.getElementById("currentFullDate").innerHTML ="Current Full Date using


JavaScript:"+date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();

document.getElementById("currentTime").innerHTML ="Current Time using


JavaScript:"+date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();

</script>

</body>

</html>

Reverse array JavaScript


<!DOCTYPE html>
<html>

<body>

<p>Reverse Array JavaScript </p>

<button onclick="reverseArrayValue()" id="btnClick">Click</button>

<p id="pId"></p>

<script>

var season = ["Summer", "Winter",

"Monsoon","Spring"];

document.getElementById("pId").innerHTML = season;

function reverseArrayValue() {season.reverse();


document.getElementById("pId").innerHTML = season;

</script>

</body>

</html>

Reverse String in JavaScript


<!DOCTYPE html>

<html>

<body>

<p>Reverse String in

JavaScript</p>Main String: "Bijay

Kumar";

<p id="reverseText"></p>

<script>

var reverseString=reverseStringMethod("Bijay Kumar");

document.getElementById("reverseText").innerHTML = "Reverse String:

"+reverseString ;function reverseStringMethod(str) {

var result = "";


for (var i = str.length - 1; i >= 0; i--

) {result += str.charAt(i);

return result;

</script>

</body>

</html>

JavaScript Open a page using window.open() method


<!DOCTYPE html>

<html>

<body>

<p>In this example we can able to see how the winow.open method is used to open a new window
pagein JavaScript </p>

<button onclick="openNewWindow()" id="btnClick">Click</button>

<script>

function openNewWindow()

window.open("https://www.google.com/);

</script>

</body>

</html>

Print page In JavaScript


<!DOCTYPE html>

<html>

<body>

<p><b>Here we will check how print method is work while clicking on button click<b></p>

<button onclick="PrintPage()" id="btnClick">Click it</button>

<script>

function

PrintPage() {

window.print();

}
</script>

</body>

</html>

get current url javascript


<!DOCTYPE html>

<html>

<body>

<p>This widow.location() method will display currently which page you are opening.</p>

<p id="pId"></p>

<script>

document.getElementById("pId").innerHTML ="The full URL of this page


is:<br>" +window.location.href;

</script>

</body>

</html>
JavaScript Date Countdown Timer
<!DOCTYPE HTML>

<html>

<head>

</head>

<body>

<p id="pId"></p>

<script>

var expiredDate = new Date("Jan 5, 2020

15:37:29").getTime();var t = setInterval(function() {

var nowdate = new Date().getTime();

var timePeriod = expiredDate - nowdate;

var days = Math.floor(timePeriod/ (1000 * 60 * 60 * 24));

var hours = Math.floor((timePeriod % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));

var minutes = Math.floor((timePeriod % (1000 * 60 * 60)) / (1000 * 60)); var seconds =

Math.floor((timePeriod % (1000 * 60)) / 1000); document.getElementById("pId").innerHTML = days +

"d " + hours +"h"+ minutes + "m " + seconds + "s ";if (timePeriod < 0)

{
clearInterval(t);

document.getElementById("pId").innerHTML =

"EXPIRED";

},

1000);

</script>

</body>

</html>
JavaScript screen height
<!DOCTYPE html>

<html>
<body>

<p>When clicking on button it is showing the Screen height</p>

<button onclick="showScreenHeight()" id="btnClick">Click</button>

<p id="pId"></p>

<script>

function showScreenHeight() {

var totalHeight = "Total Height: " + screen.height + "px";

document.getElementById("pId").innerHTML

=totalHeight;

</script>

</body>

</html>

You might also like