Archive for June, 2007

JavaScript is an Untyped Language

Posted in JavaScript, Software, Web, internet, programming on June 30, 2007 by Joey

JavaScript is an untyped language. This means that a variable can hold any datatype. It also means that a variable may be assigned data of one particular datatype and then later be given data of a different datatype. For example:

var x = 'Joey JavaScript';
x = true;
x = 1;

In the preceding example, the variable x is initialized containing the string datatype. It is than assigned a value that results in it holding the Boolean datatype. Finally, it is assigned 1, giving it the number datatype.

Many other languages are strongly typed, which means that a variable can only be associated with the datatype to which it is declared. This makes it easier to enforce datatyping rules within programs. JavaScript is untyped, however, which makes it more difficult to enforce datatyping rules but results in simplicity in developing programs quickly.

JavaScript to Validate Positive Integer While Accepting Empty String

Posted in JavaScript, Regular Expressions, Software, Web, internet, programming on June 28, 2007 by Joey

A few times at work recently we have had the need to validate a form input field as an integer while still accepting an empty sting as a valid value as well.

This can be done as follows:

var RE = /^\d*$/;
document.write(RE.test(2)); //true
document.write(RE.test('')); //true
document.write(RE.test('a')); //false

The regular expression in the preceding example specifies that the string being tested must have zero or more digit characters. Anything else is invalid.

Related articles:
JavaScript Regular Expressions
JavaScript Regular Expression Asterisk
JavaScript Regular Expression Special Characters: ^ $
JavaScript Number Validation (Integer)

JavaScript XMLHttpRequest responseText Property

Posted in JavaScript, Software, Web, internet, programming on June 27, 2007 by Joey

Once an XMLHttpRequest has been initiated, sent and a response received, the XMLHttpRequest will have a responseText property. This property contains the server response, excluding headers.

Note that responseText is not fully loaded until the XMLHttpRequest readyState is 4. The responseText property can also contain a partial portion of the final result text if readyState is 3, which means that data is in the process of being received from the server.

To access this property: If an XMLHttpRequest call is saved in a variable named objXHR, the responseText can be accessed as objXHR.responseText.

Related articles:
JavaScript XMLHttpRequest readyState values

JavaScript XMLHttpRequest responseXML Property

Posted in JavaScript, Software, Web, Web 2.0, internet, programming on June 26, 2007 by Joey

Once an XMLHttpRequest has been initiated, sent and a response received, the XMLHttpRequest will have a responseXML property, if an XML document is received from the server. This property contains the response received from the server that can be processed by JavaScript as XML. DOM methods can by applied through JavaScript on the XML. If the server does not send back XML or if the XML is not well-formed the responseXML property will contain null.

Please see my Simple Ajax Example Tutorial with ColdFusion to see a practical application of this property.

JavaScript: How to Remove All Commas From a Number

Posted in JavaScript, Regular Expressions, Software, Web, internet, programming on June 25, 2007 by Joey

The following can be used in JavaScript to remove all commas from a number:

var StartNumber = 444,555,666;
var ReplacedNumber = StartNumber.replace(/\,/g,
);
document.write(ReplacedNumber);

The preceding example uses the JavaScript replace function and utilizes a regular expression to replace all the commas in a number. In the regular expression, the backslash (\) before the comma is used as an escape character, meaning that the comma should be treated as a literal comma. The g means that the replace should be applied globally. Without this, only the first comma would be removed.

Related articles:
JavaScript replace Function
JavaScript Regular Expression Backslash

JavaScript: How to Submit a Form When a Button is Clicked

Posted in JavaScript, Software, Web, internet, programming on June 23, 2007 by Joey

Submitting a form can be accomplished with JavaScript by accessing the named form and then calling the submit method on it.

<form name="frmTest" action="somepage.html”" method="post">
First Name: <input type=
"text" name="FirstName">
<input type=
"button" onclick="document.frmTest.submit();" value="Submit Form">
</form>

The JavaScript occurs with the onclick attribute of the button input type. It uses the document object model to access the named form and calls the submit method on it.

JavaScript: How to Load a New Browser URL Page

Posted in JavaScript, Software, Web, internet, programming on June 22, 2007 by Joey

There are a few different ways in which a new page can be loaded in a browser through JavaScript.

window.document.location.href = 'http://www.joeyjavascript.com';
window.document.location = 'http://www.joeyjavascript.com';
window.location = 'http://www.joeyjavascript.com';
location = 'http://www.joeyjavascript.com';
document.location.href = 'http://www.joeyjavascript.com';
document.location = 'http://www.joeyjavascript.com';

As seen in the previous examples, both the window object and the document object have a location property that can be assigned a URL. It is generally preferable to set the location property of the window object, as the document object was originally intended as a read-only property and some older browsers still treat it as such.

Relative or absolute URLs can be assigned as the page to be loaded.

There is also a replace method that can be called on the location property, as follows:

window.location.replace('http://www.joeyjavascript.com');

Using this method will cause the requested page to replace the current page in the browser and is not a new page request. Hence, the page it replaces is no longer in the browser history.

JavaScript: Accessing getElementById in Firefox

Posted in JavaScript, Software, Web, internet, programming on June 20, 2007 by Joey

A common mistake when using the getElementById function in Firefox is failing to set the id attribute of the element being accessed. This will work in Internet Explorer because it treats the name attribute as the id attribute in the case where the id attribute is not provided. Firefox, however, is not forgiving on this point. The id attribute of an element must be provided in order to access that element via the getElementById function.

Example:

<input type=”text” name=”FirstName” id=”FirstName”>

Related articles:
Accessing Form Elements with JavaScript

Without the id=”FirstName”, this element cannot be accessed by the getElementById function in Firefox.

JavaScript Conditional Operator (?:) Question Mark, Colon

Posted in JavaScript, Software, Web, internet, programming on June 16, 2007 by Joey

JavaScript provides the conditional operator as a shortcut for an “if” statement. The conditional operator consists of a question mark (?) and a colon (:), as shown below:

var intTest = 1;
intTest == 1 ? x = 1 : x = 2;
document.write(x);

The expression preceding the question mark must evaluate to a Boolean value. If the result of this expression is true, the expression following the question mark but preceding the colon runs; otherwise, the expression after the colon runs.

JavaScript: How to Remove Comma from the End of a List

Posted in JavaScript, Regular Expressions, Software, Web, internet, programming on June 15, 2007 by Joey

Occasionally in JavaScript a list will be built using a loop. On each iteration of the loop a comma will be added to separate the current list item from the list item that will be added on the next iteration of the loop. For the final item of the list, however, there is not another value that will follow so the result is a list with an unneccessary comma at the end. This extraneous comma can easily be removed by using the JavaScript replace function, as illustrated in the following example:

var lstLetters = 'a,b,c,d,';
var lstReplace = lstLetters.replace(/\,$/,'');
document.write(lstReplace);

The regular expression in the replace function works like this:

The backslash(\) is used as an escape character so that the regular expression knows to look for a literal comma (i.e., the comma is not being used a separator).
The dollar sign ($) mean to only match a comma if it is at the end of the string. This will allow the necessary commas to remain.

Related Articles:
JavaScript Regular Expressions
JavaScript replace Function
JavaScript Regular Expression Backslash
JavaScript Regular Expression Special Characters: $ ^