Scripting Language
A scripting language, script language or extension language is a programming language that allows control of one or more applications.
Scripts are distinct from the core code of the application, as the are usually written in a different language and are often created or atleast modified by the end user. Scripts are often interpreted from source code or byte code whereas the application is typically first compiled to native machine code.
Java Script
- A scripting language i.e., a light weight programming language.
- Designed to add interactivity to HTML pages.
- Usually embedded directly into HTML pages.
- Interpreted language (execute without preliminary compilation)
Key Points
- Java and Java script are not same in both concept and design.
- Java is a powerful and much more complex programming language.
Advantages of Java Script
The advantages of Java script are as given below
(I) Java script gives HTML designers a programming tool.
- Java script can put dynamic content into an HTML page. Statement like document.write(“< h 1>” + name + ” < /h 1 >”) can write a
variable text into an HTML page.
- Java script can react to events.
- Jave script can read and write HTML elements.
- Java script can be used to validate data before it is submitted to a
- Java script can be used to detect the visitors’s browser.
(Vii) Java script can be used to create cookies.
Loops in Java Script
Loops in Java script are used to execute the same block of code a specified number of times or while a specified condition is true.
e.g.,
< html>
<body>
<script type= “text/javascript”>
var i = 0;
for (i =0 ; i <= 10 ; i + +)
{
document . write (“The number is;” + i );
document . write (“</br>”);
)
</script>
</body>
</html>
In same way, we can use while loop for this.
e.g.,
for (variable in objects)
{
code to be executed
}
The for in statement is used to loop/iterate through the elements of an array or through the properties of an object. Here, variable can be a named variable, an array element or a property of an object.
<html >
<body>
<script type= “text/javascript”>
var x;
var mycars = new Array [ ] ;
wears [0] = “Audi”
mycars [1] = “BMW”; for (x in mycars)
{
document. write (mycars [x] + “< br/>”);
}
</script>
</body> </html>
Output Audi
BMW
Java Script Functions
- To keep the browser free from executing a script when the page loads, we can put our script into a function.
A function contains code that will he executed by an event or by a call to that function.
- Functions can be defined in both the <head> and in the <body> section of a document. However, to assure that the function is read/loaded by the browser before it is called, it could be wise to put it in the <head> section.
e.g.,
<html >
<head>
<scri pt type = “text/javascri pt”> function displaymessage )
alert ( “Al 1 the Best”) ;
</script>
</head> <body> <form>
<input type = “button” value = “click me” onclick “displaymessage ( )”>
</form>
</body>
</html>
Key Points
♦ Java script is case sensitive In java script “my function” and “My Function” are not same.
White space Java scripts ignores extra spaces. We can add white spaces to our script to make it more readable. Following lines are same
name = “Arihant”;
name = “Arihant”;
- Breaking up a code line We can breakup a code line within a text string by using a backslash”\”.
e.g.,
document. write (“Hello World !”);
However, we can’t breakup a code line like this:
documents write 1 (“Hello World !”);
HTML Comments to Handle Simple Browers
Browsers that do not support Java script will display Java script content. To prevent them from doing this and as a part of thy- as standard, the HTML comment can be used to hide the Java script.
<script type “text/javascript”›
<!
document. write (“Hello”);
//..> </script>
Document. Write () It is a standard Java script command for writing output to a page or displaying text to the user.
The return Statement
The return statement is used to specify the value that is returned frorn the function.
e.g.,
<html>
<head>
<script tyre= “text/javascript”›
function product (a, b)
{
var x =a*b;
return;
}
</scri pt>
</head>
<body>
<script type= “text/javascript”›
var a = product (2, 3);
document. write (“product is =” + a);
</script>
</body>
</html>
Conditional Statements
we con refer the below example to understand this,
eg-
document. write ( “< b> Good Morning </b>”);
}
else if (time > 10 && time < 16>)
{
document . write (“< b> Good Day </b>”);
}
else
document. write (“< b> Hello world ! </b >”);
</script>
</body>
</head>
</html >
Java Script Events
Events are action that can be detected by Java script. Every element on a web page has certain events, which can trigger Java script functions.
We define the events in the HTML tags.
Example of Events
- A mouse click
- A web page or an image loading
- Mouse over a hot spot on the web page
- Selecting an input box in an HTML form
- Submitting an HTML form
(1) A key stroke
Events are normally used in combination with functions and the function will not be executed before the event occurs.
Onload and Unload
These events are triggered when the user enters or leaves the page. The onload event is often used to check the visitor’s browser type and browser version and load the proper version of the web page based on the information.
Onsubmit
This event is used to validate all form fields before submitting it,
<form method = “post” action = “abc. html”
onsubmit = “return check From ( )” >
Function check Form ( ) returns either true or false, If it returns true, the
form will be submitted. otherwise the submission will be cancelled.
Java Script switch Statement
As we have seen we can use if…else conditional statements, loops, in same way we can use switch statement in Java script to select one of many blocks of code to be executed.