Category Archives: JavaScript

The Alert Box vs The Prompt Box vs The Confirm Box in JavaScript

There are three types of pop-up boxes in JavaScript: Alert, Prompt, and Confirm boxes.

Usage:

  • Alert Box: An alert box is used when you want to ensure that information gets through to the user.
  • Prompt Box: A prompt box is used to have the user input a value before entering a page.
  • Confirm Box: A confirm box is used to have the user verify or accept something.

Continue reading

Cookies in JavaScript

A cookie is a small text file stored on the user’s computer. It can be refer to every time the user comes back to the site that has set it (if the cookie has not expired or has not been deleted). In order for a cookie to be set, the viewer mast accept cookies. Cookies must be 4 KB or less in size (that means 4000 characters). A browser can accept up to 20 cookies from a single domain. Cookies were invented by Netscape.

Table of content:

  • Setting a Cookie
  • The escape() Method
  • Allowing User Input
  • Setting an Expiration Date
  • Reading a Cookie

Continue reading

Validation of a form

Validations are performed to ensure that the user doesn’t leave any box blank.

<body>

<form method="post" action="form.php" onsubmit="return checkIt();">	<label for="yourname">Name:</label>
			<input type="text" name="yourname" id="yourname" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>

<script>
function checkIt() {
	var theName = document.getElementById("yourname").value;
	if (theName.length < 1) {
		window.alert("Name field can NOT be blank!");
		return false;
		}
	else {
		return true;
		}	
}
</script>

</body>

Continue reading

Properties and Methods of the Form Object

The table below lists the properties of the form object.

PropertyValue
actionThe value of the action attribute in the HTML form tag
elementsAn array that includes an array element for each form element in an HTML form
encodingThe value of the enctype attribute, which varies with different browsers
lengthThe value of the total number of elements in an HTML form
methodThe value of the method attribute in an HTML form tag
nameThe value of the name attribute in an HTML form tag
targetThe value of the target attribute in an HTML form tag

Continue reading

Accessing Forms

Each time you add a set of <form> and </form> tags to your HTML document, you create a form object. If you want to access a form object using JavaScript you can do so using one of the following three methods.

  • Use the forms array of the document object
  • Name the form and use the name to access the form
  • Give the form an id then use the document.getElementById() method

Continue reading

String Objects and String Literals in JavaScript


Table of content:

Continue reading

Math, Numbers, and Date Objects

Table of content:

Continue reading