JAVASCRIPT
JAVASCRIPT
JAVASCRIPT
Introduction
Browsers use their own JavaScript Engines to execute the JavaScript code. Some
commonly used browsers are listed below:
1. Light Weight
2. Dynamically Typed
3. Object-Based
4. Functional
5. Platform Independent
6. Prototype-based
7. Interpreted
Advantages of JavaScript
JavaScript has various advantages that make it very useful as a scripting language,
some are listed below:
Limitations of JavaScript
The JavaScript code can be inserted in HTML file by using the HTML <script> tag.
When an HTML document is loaded with the <script> tag in the web browser, the
browser processes the content enclosed inside the script tag as JavaScript code.
The script tag can contain either scripting statements or refer to an external JavaScript
file. The script tag provides a src attribute that allows us to add a reference to an
external script file.
With the HTML DOM, JavaScript can access and change all the elements of an
HTML document.
When a web page is loaded, the browser creates a Document Object Model of the
page.
HTML:
The HTML DOM is a standard object model and programming interface for
HTML. It defines:
Similarly, we can use the <script> tag to directly add the JavaScript code too, like this:
<script type="text/javascript">
// JS code here
</script>
You can use script tag inside an HTML web page to add JavaScript code in the
following ways:
Let's put the script tag inside the HTML head element. The script placed inside
the head element is loaded with the webpage and gets executed if any defined event
occurs.
The code given below shows how to use the <script> tag inside the <head> element of
an HTML document to add some JavaScript code.
<html>
<head>
<title>my first JavaScript</title>
<script>
function text()
</script>
</head>
<body>
</body>
</html>
You can place a script tag inside the body element of an HTML document too. The
script tag inside the body element runs when a web page starts loading in a web
browser. Below is the code to show you how to place a <script> element inside
the <body> element of an HTML document:
<html>
<head>
</head>
<body>
<script>
function text()
</script>
</body>
</html>
To link the external file, we can provide its location (URL) in the src attribute of the
script tag.
The code given below shows you how to link an external JavaScript file with an
HTML document.
<html>
<head>
<script src="jsfile.js"></script>
</head>
<body>
</body>
</html>
function text()
{
document.getElementById("script").innerHTML = "This text has been
written inside the JavaScript.";
JavaScript Syntax
JavaScript has its own syntax and programming style. Syntax of a language defines
rules of writing the code in that language, what is the correct way and what is not.
Semicolons are used to terminate the JavaScript code statements. A code statement
is a line of code and we can mark the end of each code line by adding a semicolon. In
languages like C, C++, Java, etc. we use a semicolon at the end of each code
statement.
Example
Eg:
var a=1;
let a=1;
const a=1;
Example
var i = 10;
var j = 20;
var marks;
var Marks;
var MARKS;
4. JavaScript Comments
Let's take an example and understand how we can add these in our JavaScript code.
<html>
<head>
<script>
/*
*/
var x = 10;
var y = 2*x;
</script>
</head>
<body>
</body>
</html>
5. JavaScript Statements
<html>
<head>
<title>JavaScript Statement</title>
</head>
<body>
<script>
</script>
</body>
</html>
JavaScript lets you write into an HTML element by using innerHTML property. We
can add anything we want, it can be a text message, some HTML element or anything
else.
To do that first you need to provide a specific Id to the HTML element that you
want to access by the JavaScript code.
Let's take an example, in this example, id attribute is used to identify the HTML
element and innerHTML property is used to set content to it.
<html>
<head>
<script>
function addText()
{
document.getElementById("script").innerHTML= "This text has been written using
the JavaScript.";
</script>
</head>
<body>
</body>
</html>
JavaScript lets you write any output into the HTML webpage by using
the document.write() method. By using this method you can directly write output to
the HTML page.
<html>
<head>
</head>
<body>
<script>
</body>
</html>
There are certain websites that give you alert messages when you access them or when
you perform some action you see the output message in alert boxes. You can also
make your webpage to send alert messages to notify something to the user using
JavaScript, to use this feature you need to use the window.alert() method.
<head>
</head>
<body>
<script>
</script>
</body>
</html>
JavaScript also lets you create console logs which can be seen in the browser's
developers' tools(Console) for debugging purposes. The statement written inside a
console log will be executed but would not be displayed in the browser instead it will
be displayed inside the console of the browser.
<html>
<head>
<script>
console.log(2+3);
</script>
</head>
<body>
</body>
</html>
Following is the syntax for declaring a variable and assigning values to it.
var variable_name;
// or
let variable_name;
var x, y, z;
Now let's take a simple example where we will declare a variable and then assign it a
value.
A JavaScript can be executed when an event occurs, like when a user clicks on an
HTML element.
To execute code when a user clicks on an element, add JavaScript code to an HTML
event attribute:
Mouse events:
mouseover onmouseover When the cursor of the mouse comes over the
element
Form events:
The onmouseover event triggers when you bring your mouse over any element and
the onmouseout triggers when you move your mouse out from that element.
<html>
<head>
<script type = "text/javascript">
function over() {
document.write ("Mouse Over");}
function out() {
document.write ("Mouse Out");}
</script>
</head>
<body>
<p>Bring your mouse inside the division to see the result:</p>
<div onmouseover = "over()" onmouseout = "out()">
<h2> This is inside the division </h2>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The onfocus and blur Events</h2>
<p>When you enter the input field, a function sets the background color to yellow.</p>
<p>When you leave the input field, a function sets the background color to red.</p>
function blurFunction() {
document.getElementById("myInput").style.background = "red";
}
</script>
</body>
</html>
JavaScript provides a way to validate form's data on the client's computer before
sending it to the web server. Form validation generally performs two functions.
• Basic Validation
<label for="">Name</label>
<label for="">Mobile</label>
</form>
<script>
function check(){
name=document.getElementById('name').value;
mob=document.getElementById('mob').value;
if(name==""){
alert("please enter the name");
return false;
}
else if(mob==""){
alert("Please enter mobile number");
return false;
}
}
</script>
Email validation
gmail=/^([a-z0-9])+\@([a-z]{5})+\.([a-z]{3})+$/
if (!gmail.test(em)){
return False;
num=/^([0-9]{10})$/
if (!num.test(mob)){
return False;