Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Regular Expressions, Software, Web | 3 Comments »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web, Web 2.0 | Leave a Comment »
Posted 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 = FormNumber.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 [...]
Filed under: Internet, JavaScript, Programming, Regular Expressions, Software, Web | 3 Comments »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | 1 Comment »
Posted 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. [...]
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | 5 Comments »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Regular Expressions, Software, Web | Leave a Comment »