Background
I have often observed that users put their problems on the CodeProject forum related to JavaScript. They don't know the exact use of JavaScript. Why should we use it? Where to define it?
In this article, I will explain what JavaScript is, why JavaScript is used, how to use JavaScript for validation, functions, and properties, and some easy functions that you can copy and paste into your code. This resource is entirely about JavaScript.
Introduction
JavaScript is the most popular scripting language used in almost all browsers. The primary use of JavaScript is to validate user input. Besides that, JavaScript can be used for designing CSS, data manipulation, and more.
Including JavaScript to Page
JavaScript can be used with HTML, DHTML, XML, XHTML, ASP, JSP, ASPX, PHP, etc. JavaScript is defined in a separate block of HEAD. Let's see the following example; it will give you a clear idea.
<html>
<head>
<script language="JavaScript"> function CallMe(){alert("Yes..first POP up from javascript");return false;}</script>
</head>
<body> Welcome to javascript example!<input type="submit" id="sub1" onclick="return CallMe();"> </body>
</html>
Let's travel with the above code,
- JavaScript defined under HEAD tag
- We can write functions in JavaScript
- the function is a reserved keyword
- the alert is used for POP message
- return false indicates the page should not submit to the server (means code execution will stop at that point)
- We can call the JavaScript function using the onclick event
- Like VBScript, we cannot change the caption of the POPup box in JavaScript
Using the Document Object
the document is the most commonly used object in JavaScript. We can collect any control from the page using a document as an object. Let's take an example.
Validating Page for empty textbox using document object
[now I am just giving an example through functions, you can call them on your page],
function ValidateME()
{
if (document.getElementById("txtUserName").value == "")
{
alert("Please enter user Id rraannaammeett"); // Give alert to user
document.getElementById("txtUserName").focus(); // Set focus on textbox
return false;
}
}
Change the forecolor/backcolor of the page using the document object
function ChangeME()
{
document.bgColor = "red"; //change the backcolor to RED
document.fgColor = "#338844"; //change the forecolor
}
Write on a page using the document object
function WriteME()
{
document.write("I can write on page");
}
Access form controls using 'ALL' property (ALL property is a browser(IE) specific)
function GetControlValue()
{
alert(document.all.txtUser.Value);
}
document object has numerous functions and properties. Here are some of them,
- alinkColor - Specifies the color of activated links
- domain - Gives domain name under which served you used for security purposes
- title - Specifies the title of a document
- cookie - A string containing the name/value pair of cookies in the document
- FileSize - Return the file size of the current document
- URL - A string that specifies the complete URL of the document
- cookie - A string containing the name/value pair of cookies in the document
POP-UPs
The most famous part of JavaScript which the prompt user from the client side. Let's see how we can generate POP-UPs with some lines of code.
using alert
ALERT can be used to alert the user. A simple messagebox is accepted by all browsers.
Here is a small example
function giveAlert()
{
alert("WOW ! this is alert");
return false;
}
using confirm
CONFIRM can be used to confirm user decisions. A simple messagebox with OK and CANCEL buttons. This function returns a boolean value. Here is a small example,
function askFor()
{
if (confirm("Are you sure rraannaammeett"))
alert("Yes, i am sure rraannaammeett")
else
alert("No, i am not sure rraannaammeett")
}
using Prompt
This is used to accept input from users,
function EnterChoice()
{
var reply = prompt("what is your name", "Enter your name")
alert("HI" + reply);
}
Here var is the datatype in JavaScript like integer, string, and "reply" is the variable.
Event Handling
JavaScript supports several events; here we go for the most common/used events.
onClick
This event is supported mainly by all controls of the webform.
onClick handlers execute something only when users click buttons, links, checkboxes, radiobutton, etc.
<script>
function callME()
{
alert("yes..OnClick called!")
}
</script>
<form>
<input type="button" value="Click Me" onclick="callME()">
</form>
onchange
This event is fired by the combo box while changing items in the combo list. Here is an example of how to get a selected item from the dropdownlist.
<script>
function getItem()
{
alert("you select " + document.getElementById("idDDL").value)
}
</script>
<form>
<asp:Dropdownlist id="idDDL" runat="server" onchange="getItem()">
<listItem>a<listItem>
</asp:Dropdownlist>
</form> 
Join the conversation! Your thoughts help the community grow.