JavaScript Notes
JavaScript Notes
What is JavaScript
JavaScript (js) is a light-weight object-oriented programming language
which is used by several websites for scripting the webpages. It is an interpreted,
not full-fledged programming language that enables dynamic interactivity on
websites when applied to an HTML document. It was introduced by Brenden Eich
in the year 1995 for adding programs to the webpages in the Netscape Navigator
browser. Since then, it has been adopted by all other graphical web browsers.
With JavaScript, users can build modern web applications to interact directly
without reloading the page every time. The traditional website uses js to provide
several forms of interactivity and simplicity.
Features of JavaScript
Application of JavaScript
o Client-side validation,
o Dynamic drop-down menus,
o Displaying date and time,
o Displaying pop-up windows and dialog boxes (like an alert dialog box,
confirm dialog box and prompt dialog box),
o Displaying clocks etc.
Advantages:
• Fast speed: JavaScript is executed on the client side that’s why it is very
fast.
• Easy to learn: JavaScript is easy to learn. Any one which has basic
knowledge of programming can easily lean JavaScript.
• Versatility: It refers to lots of skills. It can be used in a wide range of
applications.
• Browser Compatible: JavaScript supports all modern browsers. It can
execute on any browser and produce same result.
• Server Load: JavaScript reduce the server load as it executes on the client
side.
• Rich interfaces: JavaScript provides the drag and drop functionalities which
can provide the rich look to the web pages.
• Popularity: JavaScript is a very popular web language because it is used
everywhere on the web.
Disadvantages:
• Code Visibility: JavaScript code is visible to everyone and this is the biggest
disadvantage of JavaScript.
• Stop Render: One error in JavaScript code can stop whole website to
render.
JavaScript Events:
By using JavaScript, we have the ability to create dynamic web pages. Events are
actions that can be detected by JavaScript.
When the page loads, it is called an event. When the user clicks a button, that
click too is an event. Other examples include events like pressing any key, closing
a window, resizing a window, etc. We define the events in the HTML tags.
Mouse events:
mouseover onmouseover When the cursor of the mouse comes over the element
mousedown onmousedown When the mouse button is pressed over the element
mouseup onmouseup When the mouse button is released over the element
mousemove onmousemove When the mouse movement takes place.
Keyboard events:
Keydown & Keyup onkeydown & onkeyup When the user press and then release the key
Form events:
change onchange When the user modifies or changes the value of a form
element
Window/Document events
load onload When the browser finishes the loading of the page
unload onunload When the visitor leaves the current webpage, the browser
unloads it
resize onresize When the visitor resizes the window of the browser
Click Event
<html>
<head> Javascript Events </head>
<body>
<script language="Javascript" type="text/Javascript">
function clickevent()
{
document.write("This is JavaTpoint");
}
</script>
<form>
<input type="button" onclick="clickevent()" value="Who's this?"/>
</form>
</body>
</html>
Output:
MouseOver Event
<html>
<head>
<h1> Javascript Events </h1>
</head>
<body>
<script language="Javascript" type="text/Javascript">
function mouseoverevent()
{
alert("This is JavaTpoint");
}
</script>
<p onmouseover="mouseoverevent()"> Keep cursor over me</p>
</body>
</html>
Output:
Focus Event
<html>
<head> Javascript Events</head>
<body>
<h2> Enter something here</h2>
<input type="text" id="input1" onfocus="focusevent()"/>
<script>
function focusevent()
{
document.getElementById("input1").style.background=" red";
}
</script>
</body>
</html>
Output:
Keydown Event
<html>
<head> Javascript Events</head>
<body>
<h2> Enter something here</h2>
<input type="text" id="input1" onkeydown="keydownevent()"/>
<script>
function keydownevent()
{
document.getElementById("input1");
alert("Pressed a key");
}
</script>
</body>
</html>
Output:
Load event
<html>
<head>Javascript Events</head>
</br>
<body onload="window.alert('Page successfully loaded');">
<script>
document.write("The page is loaded successfully");
</script>
</body>
</html>
Load resize
<html>
<head>
<script>
function hello()
{
alert("Hello world")
}
</script>
</head>
<body onresize="hello()">
<p>This is a pharagraph</button>
</body>
</html>
https://www.youtube.com/watch?v=R7mu7nKFc7w
JavaScript For loop
The JavaScript for loop iterates the elements for the fixed number of times. It
should be used if number of iteration is known. The syntax of for loop is given
below.
Example:
<html>
<head>
</head>
<body>
<script>
var j;
for(j=1;j<6;j++)
{
document.write(j," ","Hi for loop!<br>") //for tab space in html use
 (4spaces)
}
</script>
</body>
</html>