JavaScript Examples PDF
JavaScript Examples PDF
51 JavaScript
Examples
[Type here]
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
Table of Contents
About TSInfo Technologies: .......................................................................................................................... 4
About Authors:.............................................................................................................................................. 4
What is JavaScript? ....................................................................................................................................... 5
Editors For JavaScript .................................................................................................................................... 5
How to Start with JavaScript? ....................................................................................................................... 5
Example-1: Display Alert in JavaScript .......................................................................................................... 7
Display Conformation box using JavaScript: ............................................................................................. 8
Display Prompt box using JavaScript: ....................................................................................................... 9
Example-2: mouseover and mouseout in javascript................................................................................... 10
Example-3: Use onkeypress In JavaScript to Display Alerts ........................................................................ 12
Example-4: JavaScript Validation Examples ................................................................................................ 13
Textbox required validation in javascript: .............................................................................................. 13
Email validation in JavaScript: ................................................................................................................. 14
Letter Validation in JavaScript: ............................................................................................................... 15
Number Validation in JavaScript: ............................................................................................................ 16
Letter and Number Validation in JavaScript: .......................................................................................... 17
IP Address Validation in JavaScript: ........................................................................................................ 18
Example-5: Retrieve Today’s Date in JavaScript ......................................................................................... 19
Example-6: Reverse array javascript ........................................................................................................... 20
Example-7: JavaScript concate or merge two Arrays ................................................................................. 21
Example-8: Reverse String in JavaScript ..................................................................................................... 22
Example-9: JavaScript Open a page using window.open() method ........................................................... 23
Example-10: if else statement in JavaScript: .............................................................................................. 23
Example-11: Print page In JavaScript: ......................................................................................................... 24
Example-12: Insert element in array javascript .......................................................................................... 25
Example-13: get current url javascript........................................................................................................ 26
Example-14: getElementsByClassName() method in JavaScript................................................................. 27
Example-15: getElementByName() method in JavaScript .......................................................................... 27
Example-16: Disable Browser back and forward button in browser using JavaScript ............................... 28
Disable back button in browser using javascript ................................................................................ 28
Disable Forward button in browser using JavaScript:......................................................................... 29
Example-17: Disable and Enable Dropdown List using JavaScript: ............................................................. 30
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
TSInfo Technologies works in technologies like SharePoint, Office 365, Azure, PowerBI, Nintex etc.
For any kind of SharePoint project work or corporate training contact us:
https://www.EnjoySharePoint.com || https://www.SharePointSky.com
About Authors:
Bijay Kumar Sahoo:
Bijay Kumar Sahoo is a Microsoft MVP in Office Servers and Services category specialized in SharePoint.
He has more than 11 years of experience in Microsoft technologies like SharePoint, Office 365,
SharePoint Online, Azure, PowerBI, Nintex etc. Bijay is a SharePoint consultant, trainer and founder of
TSInfo Technologies a SharePoint development company in India. Bijay also focuses on SharePoint
technical blogging and blogs in EnjoySharePoint.com and SharePointSky.com.
Padmini Podili:
Padmini is having 2+ years of experience in SharePoint Online Office 365, PowerShell etc. She is working
with TSInfo Technologies Pvt Ltd, Bangalore.
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
What is JavaScript?
JavaScript is a lightweight object-based scripting programing language. Basically to add the functionality
in HTML code javascript is used. Because of the JavaScript, web pages become more interactive with the
user. Mostly JavaScript use for game development and mobile application development. The JavaScript
make the HTML form dynamic. The JavaScript is used to create client-side dynamic pages.
We can run JavaScript with the help of any browser. We can use Javascript code in the HTML file using
the Script tag. When we run the HTML code in the browser then the javascript also run. The main
advantage of JavaScript scripting language is all browser like IE, Chrome, Mozilla, Opera etc supports
JavaScript. JavaScript runs on any operating system like Windows, Linux, mac anything.
Notepad
Notepad++
Sublime Text
Visual Studio Code
Atom
BBEdit
Komodo Edit
Emacs etc.
Example:
<script>
</script>
We can put any number of scripts inside an HTML document, either in the <body> or <head> section of
the HTML page or in both tags.
<!DOCTYPE html>
<html>
<head>
<script>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
function demoFunction() {
alert("JavaScript Examples")
</script>
</head>
<body>
<script>
function myFunction() {
</script>
</body>
</html>
We can also store JavaScript code in an external .js file and call that file inside our HTML Code. We
should not write all JavaScript code inside the HTML file.
We can store below script code inside a .js file like (51JavaScriptExamples.js)
function showAlert() {
<!DOCTYPE html>
<html>
<body>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<script src="myScript.js"></script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
First name:<br>
<br>
Last name:<br>
<br>
<br>
<script>
function showAlert() {
alert("Created Account");
</script>
</body>
</html>
The output: We can see in the output one alert message is coming and telling that “Created Account”
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
function showConformationMessage(){
if (name == true){
else{
</script>
</body>
</html>
Output:
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
Output is showing a conformation box which is asking for “Are you want to use the account”. We can
click on “Ok” or “Cancel”
<!DOCTYPE html>
<html>
<body>
<p id="pId"></p>
<script>
function showPromptBox() {
var text;
} else {
document.getElementById("pId").innerHTML =text;
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
Output:
<!DOCTYPE html>
<html>
<body>
<img onmouseover="largePictureOnMouseOver(this)"onmouseout="smallPictureOnMouseOut(this)"
border="0" src="https://onlysharepoint2013.sharepoint.com/:i:/r/sites/Raju/tsinfo/SiteAssets/cool-
javascript-examples.jpg?csf=1&e=igNfKB" width="32" height="32">
<p>The large picture will display when mouse pointer over the image and when mouse pointer is moved
out of picture</p>
<script>
function largePictureOnMouseOver(p) {
p.style.height = "64px";
p.style.width = "64px";
function smallPictureOnMouseOut(p) {
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
p.style.height = "32px";
p.style.width = "32px";
</script>
</body>
</html>
Similarly in the below JavaScript example, we can use onmouseout which will display larger picture
when user remove mouse pointer from a picture.
<!DOCTYPE html>
<html>
<body>
<img onmouseout="largePictureOnMouseOver(this)"onmouseover="smallPictureOnMouseOut(this)"
border="0" src="https://onlysharepoint2013.sharepoint.com/:i:/r/sites/Raju/tsinfo/SiteAssets/cool-
javascript-examples.jpg?csf=1&e=igNfKB" width="32" height="32">
<p>The small picture will display when mouse pointer over the image and when mouse pointer is moved
out of picture, large picture is displaying</p>
<script>
function largePictureOnMouseOver(p) {
p.style.height = "64px";
p.style.width = "64px";
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
function smallPictureOnMouseOut(p) {
p.style.height = "32px";
p.style.width = "32px";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
function onKeyDown() {
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<html>
<body>
<script type='text/javascript'>
function textBoxValidation()
if(document.getElementById('txtBox').value.trim()== "")
txtBox.focus();
return false;
return true;
</script>
<form>
</form>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</body>
</html>
<html>
<body>
<script type='text/javascript'>
function emailValidation()
if(document.getElementById('txtEmail').value.match(emailIdValue))
return true;
else
txtEmail.focus();
return false;
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
<form>
</form>
</body>
</html>
<html>
<body>
<script type='text/javascript'>
function letterValidation()
if(document.getElementById('txtletter').value.match(letterValue))
return true;
else
txtletter.focus();
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
return false;
</script>
<form>
</form>
</body>
</html>
<html>
<body>
<script type='text/javascript'>
function onlyNumberValidation()
if(document.getElementById('txtletter').value.match(letterAndNumberValue))
return true;
else
txtletter.focus();
return false;
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
<form>
</form>
</body>
</html>
<html>
<body>
<script type='text/javascript'>
function letterAndNumberValidation()
if(document.getElementById('txtletter').value.match(letterAndNumberValue))
return true;
else
txtletter.focus();
return false;
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
<form>
</form>
</body>
</html>
<html>
<body>
<script type='text/javascript'>
function ipAddressValidation()
if(document.getElementById('txtletter').value.match(ipAddressPattern))
return true;
else
txtletter.focus();
return false;
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
<form>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="currentDateDate"></p>
<p id="currentMonth"></p>
<p id="currentYear"></p>
<p id="currentFullDate"></p>
<p id="currentTime"></p>
<script>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
</body>
</html>
Once you run code, you can see the output like below:
<!DOCTYPE html>
<html>
<body>
<p id="pId"></p>
<script>
document.getElementById("pId").innerHTML = season;
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
function reverseArrayValue() {
season.reverse();
document.getElementById("pId").innerHTML = season;
</script>
</body>
</html>
We can able to see how the code is helpful to change the sequence of season.
Below is the JavaScript code which will concat both the arrays.
<!DOCTYPE html>
<html>
<body>
<p>We will see after clicking the button how two array will join</p>
<p id="pId"></p>
<script>
function concateTwoArray() {
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
document.getElementById("pId").innerHTML = totalDay ;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="reverseText"></p>
<script>
function reverseStringMethod(str) {
result += str.charAt(i);
return result;
</script>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</body>
</html>
<!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 page
in JavaScript </p>
<script>
function openNewWindow() {
window.open("https://www.tsinfotechnologies.com/");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<p>In this code we will check particular year is a leap year or not</p>
<script type="text/javascript">
var daysInFebruary=29;
if( daysInFebruary==29 ){
else
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p><b>Here we will check how print method is work while clicking on button click<b></p>
<script>
function PrintPage() {
window.print();
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="pId"></p>
<script>
document.getElementById("pId").innerHTML = days;
function pushElementToArray() {
days.push("Saturday");
document.getElementById("pId").innerHTML = days;
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>This widow.location() method will display currently which page you are opening.</p>
<p id="pId"></p>
<script>
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<body>
<script>
function RetriveByClassName() {
var x = document.getElementsByClassName("myclass");
alert(x[0].innerHTML);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<script>
function retriveElementByName(){
var i;
if (animalName[i].type == "checkbox") {
animalName[i].checked = true;
</script>
</body>
</html>
<!DOCTYPE html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<html>
<body>
<script>
function disableBackButton() {
window.history.back();
</script>
</body>
</html>
<html>
<head>
<script>
function disableTheForwardButton() {
window.history.forward()
</script>
</head>
<body>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<form>
<select id="selectAnimal">
<option>Cats</option>
<option>Dogs</option>
</select>
</form>
<script>
function disableDropDown() {
document.getElementById("selectAnimal").disabled = true;
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<body>
<form>
<select id="selectAnimal">
<option>Cats</option>
<option>Dogs</option>
</select>
</form>
<script>
function enableDropDown() {
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
document.getElementById("selectAnimal").disabled = false;
</script>
</body>
</html>
<html>
<body >
<p>In this Example we can able to see how to disable mouse right click in JavaScript.</p>
<script type=”text/javascript”>
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = function() {
return false;
};
else {
document.onmouseup = function(x) {
if (x.which == 2 || x.which == 3) {
return false;
};
document.oncontextmenu = function() {
return false;
};
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<p id="pId"></p>
<script>
var t = setInterval(function() {
if (timePeriod < 0)
clearInterval(t);
document.getElementById("pId").innerHTML = "EXPIRED";
},
1000);
</script>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</body>
</html>
Once you run the code you can see the results like below:
<!DOCTYPE html>
<html>
<body>
<script>
function showCheckBoxAlert() {
var x = document.getElementById("chkBox");
if(!x.checked)
return false;
else
return false;
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
</body>
</html>
If user does not check the check box, it will display an alert message.
Here my url
is:https://onlysharepoint2013.sharepoint.com/sites/Raju/tsinfo/SitePages/51JsExample.aspx?PageView
=Shared&InitialTabId=Ribbon.WebPartPage&VisibilityContext=WSSWebPartPage
VisibilityContext=”WSSWebPartPage”
<html>
<body>
<script>
function GetParameterValues(param) {
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
if (urlparam[0] == param) {
return urlparam[1];
alert(urlparam[1]);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction(Gender) {
alert(Gender.value);
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<body>
<script>
function selectTheMariedButton() {
var x = document.getElementById("radiomarried");
x.checked = true;
alert("Married is selected");
function selectTheUnmariedButton() {
var y = document.getElementById("radioUnmarried");
y.checked = true;
alert("Unmarried is selected");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<body>
<p id="pId"></p>
<script>
function showScreenHeight() {
document.getElementById("pId").innerHTML =totalHeight;
</script>
</body>
</html>
<html>
<head>
<script type="text/JavaScript">
function AutoRefresh( t ) {
setTimeout("location.reload(true);", t);
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
</head>
<body onload="JavaScript:AutoRefresh(3000);">
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
function convert(degree) {
var p;
if (degree == "C") {
p = document.getElementById("txtCelcius").value * 9 / 5 + 32;
document.getElementById("txtFahrenhit").value = Math.round(p);
} else {
p = (document.getElementById("txtFahrenhit").value -32) * 5 / 9;
document.getElementById("txtCelcius").value = Math.round(p);
</script>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="pId"></p>
<script>
document.getElementById("pId").innerHTML = date;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<head>
<style>
div {
width: 200px;
height: 100px;
overflow: scroll;
</style>
</head>
<body>
<script>
var x = 0;
function scrollDownEvent() {
document.getElementById("pSpan").innerHTML = x += 1;
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<style>
#myBigContainer {
width: 600px;
height: 500px;
position: relative;
background: pink;
#mySmallContainer {
width: 100px;
height: 50px;
position: absolute;
background-color: black;
</style>
<body>
<p>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</p>
<div id ="myBigContainer">
<div id ="mySmallContainer"></div>
</div>
<script>
function javaScriptAniminationForMove() {
var p = 0;
function frame() {
if (p == 500) {
clearInterval(idValue);
else
p++;
element.style.top = p+ 'px';
element.style.left = p + 'px';
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<body>
<h2>51 Top JavaScript Example: Play and stop a video using JavaScript</h2>
<p>When we click on Play button the video will play and when click on stop button the video will
stop</p>
<source src="https://onlysharepoint2013.sharepoint.com/sites/Raju/tsinfo/SiteAssets/WaterFall.mov"
type="video/mp4">
</video>
<script>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
function playVideo()
video.play();
function stopVideo()
video.pause();
</script>
</body>
</html>
You can see below example, by clicking on a button video is playing and in another button click video we
are pausing the video using JavaScript.
<!DOCTYPE html>
<html>
<head>
<style>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
#divId{
width: 300px;
height: 300px;
background-color:blue;
color: white;
</style>
</head>
<body>
<div id="divId">
</div>
<script>
function changeBackgroundColour() {
document.getElementById("divId").style.backgroundColor = "red";
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<p>In this example we can able to see in every 5 second the page colour is changing using
JavaScript</p>
<style>
#fullScreenId {
position: relative;
height: 100%;
width: 100%;
</style>
<body>
<div id="fullScreenId"></div>
<script>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
function getRandomColorInEach5Sec() {
return randomColor;
function chnageTheFullScreenColour(){
div.style.backgroundColor= getRandomColorInEach5Sec();
setInterval(chnageTheFullScreenColour,1000);
</script>
</body>
</head>
</html>
<!DOCTYPE html>
<html>
<body>
<p>After Click on button after 3 second we can able to see the text</p>
<p id="pId"></p>
<script>
function displayTextIn3SecondAfterClickOnButton() {
</script>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>By using Math.max.apply method we can find out the maximum array value</p>
<p id="pId"></p>
<script>
document.getElementById("pId").innerHTML = maximumArayValue(marks);
function maximumArayValue(array) {
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<body>
<p>By combining sort() and reverse() you can sort an array in descending order.</p>
<button onclick=”sortAndReverseArrayValue()”>Click</button>
<p id=”pId”></p>
<script>
var banksOfIndis =
[“CentralBankOfIndia”,”AndhraBank”,”BankOfBaroda”,”CanaraBank”,”AllhabadBank”];
document.getElementById(“pId”).innerHTML = banksOfIndis;
function sortAndReverseArrayValue() {
banksOfIndis.sort();
banksOfIndis.reverse();
document.getElementById(“pId”).innerHTML = banksOfIndis;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<body>
<p>In this example we can able to check the index value of largest array value</p>
<script>
function IndexValueOfALargestArrayValue(array){
var startingValue = 1;
var maximumValue = 0;
maximumValue = startingValue;
return maximumValue;
</script>
</body>
</html>
<html>
<body>
<p id="pId"></p>
<script>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
var x;
try {
x = y + 1;
document.write(x);
catch(err) {
document.write(err);
</script>
</body>
</html>
[code]
This JavaScript example shows how we can return boolean value of an integer express in JavaScript.
[code]
<!DOCTYPE html>
<html>
<body>
<script>
function boleanValueOfANumber()
var x = 100;
var y=10;
var booleanValue=Boolean(100/10);
document.write(booleanValue);
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
function boleanValueOfANumber()
var x = 100;
var y=10;
var booleanValue=Boolean(100/10);
document.write(booleanValue);
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function checkArrayValueOrNot() {
var x = document.getElementById("demo");
x.innerHTML = Array.isArray(states);
</script>
</body>
</html>
<html>
<head>
<body>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<script>
function printStarInPyramidStructure()
var rows=5;
for(var i=1;i<=rows;i++)
for(var j=1;j<=i;j++)
document.write(" * ");
document.write("<br/>");
</script>
</body>
</head>
</html>
<html>
<head>
</head>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<p id="tableId"></p>
<script type="text/javascript">
function displayNumberInTable(){
var n = Number(document.getElementById('txtNumber').value);
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<html>
<head>
<style>
.name{
float:center;
margin:100px;
.tooltip{
position:fixed;
margin:5px;
width:200px;
height:50px;
display:none;
</style>
</head>
<body>
FirstName:
</div>
</div>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
Qualification:
</div>
</div>
Permanenet Adress:
</div>
</div>
<script>
element.style.display="block";
element.style.display="";
</script>
</body>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<body>
<script>
function ReloadThepage() {
location.reload();
</script>
</body>
</html>
JavaScript Break:
This javascript example explains how to use break keyword.
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function breakStatementExample()
var x;
if(x==10)
break;
if (x === 0)
document.write(x +" "+ "is"+" " + "an" + " " + "even number"+ "<br>");
else if (x % 2 === 0) {
document.write(x + " "+ "is"+ " " + "an" + " " + "even number"+ "<br>");
else {
document.write(x + " "+ "is"+ " " + "a" + " " + "odd number"+ "<br>" );
</script>
</body>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
</html>
JavaScript Continue:
This javascript example explains how you can use Continue keyword.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function breakStatementExample()
var x;
if (x === 10) {
continue;
if (x === 0)
document.write(x +" "+ "is"+" " + "an" + " " + "even number"+ "<br>");
else if (x % 2 === 0) {
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
document.write(x + " "+ "is"+ " " + "an" + " " + "even number"+ "<br>");
else {
document.write(x + " "+ "is"+ " " + "a" + " " + "odd number"+ "<br>" );
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>This example will display eligible for blod donation or not, Enter age in the text box</p>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<p id="pId"></p>
<script>
function exampleOfTernaryOpearator() {
minimumAge = Number(document.getElementById("txtage").value);
if (isNaN(minimumAge)) {
} else {
donateable= (minimumAge >18) ? "Eligibel for blood donation" : "Not eligibel for blod donation";
document.getElementById("pId").innerHTML =donateable;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="pId"></p>
<script>
// Create an object:
var MobilePhone= {
brandName: "Samsung",
modelNumber: 35566,
size:6,
fullName : function() {
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
return this.brandName +" "+"mobilephone"+ " " +"model number"+" "+ this.modelNumber;
};
document.getElementById("pId").innerHTML = MobilePhone.fullName();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="pId"></p>
<script>
function formValidation() {
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
if (!inputValue.checkValidity()) {
document.getElementById("pId").innerHTML = inputValue.validationMessage;
else
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<select id="strongStateInIndia">
<option value="Karnataka">Karnataka</option>
<option value="Uttarpradesh">Uttarpradesh</option>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<option value="Andhrapradesh">Andhrapradesh</option>
</select>
<script>
function setTheDropdownValue() {
document.getElementById("strongStateInIndia").value = "Karnataka";
</script>
</body>
</html>
<html>
<head>
<body>
<p>Top 51 JavaScript Example:Pick a number and as per the number automatically image is
displaying</p>
<img name="wave"
src="https://onlysharepoint2013.sharepoint.com/sites/Raju/tsinfo/SiteAssets/Wavepic.jpg"
width="260" height="250" >
<script language="JavaScript">
var waveImage=new
Array("https://onlysharepoint2013.sharepoint.com/sites/Raju/tsinfo/SiteAssets/leavespic.jpg",
"https://onlysharepoint2013.sharepoint.com/sites/Raju/tsinfo/SiteAssets/waterfallFromTop.jpg");
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
n--;
document.images["wave"].src = waveImage[n];
</script>
</body>
</html>
<html >
<head>
</head>
<body>
<select id="ddlCountry">
</select>
<script type="text/javascript">
function populateCountryList() {
var country = [
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
{ Name: "India" },
{Name: "China" },
{ Name: "USA" },
{ Name: "Australia" }
];
option.innerHTML = country[i].Name;
ddlCountry.options.add(option);
</script>
<!DOCTYPE html>
<html>
<body>
<script>
function getBrowserName() {
if((navigator.userAgent.indexOf("Opera") || navigator.userAgent.indexOf('OPR')) != -1 )
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
alert('Opera');
else if(navigator.userAgent.indexOf("Chrome") != -1 )
alert('Google Chrome');
alert('Safari');
else if(navigator.userAgent.indexOf("Firefox") != -1 )
alert('Mozilla Firefox');
alert('Internet Explorer');
else
alert('Other');
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
<!DOCTYPE html>
<html>
<body>
<p id="pId"></p>
<script>
var banksOfIndis =
["CentralBankOfIndia","AndhraBank","BankOfBaroda","CanaraBank","AllhabadBank"];
function banksNameInAlphabaticalOrder() {
banksOfIndis.sort();
document.getElementById("pId").innerHTML = banksOfIndis;
</script>
</body>
</html>
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253
TSInfoTechnologies.com EnjoySharePoint.com SharePointSky.com
Conclusion:
This javascript tutorial explains about 51 useful javascript examples which will be helpful to professional
who wants to learn JavaScript from basics to advances level.
TSInfo Technologies (OPC) Pvt Ltd | Flat G-04, SJR Residency, Devarabeesanahalli, Bangalore,
India, 560103 | Email: info@tsinfotechnologies.com | Mob: +91-9916854253