Archive for May, 2007

What is Ajax?

Posted in JavaScript, Software, Web, internet, programming on May 29, 2007 by Joey

Ajax is shorthand for Asynchronous JavaScript and XML. Ajax is used primarly in web applications to get or post data on a web page without having to reload the entire page. It is utilized primarly as a usability technique. When used correctly, it can greatly enhance productivity for users of web applications as the wait time between full page loads can be drastically reduced, or even eliminated.

Ajax is not a single programming language. It combines the usage of multiple programming languages, most notably JavaScript, the Document Object Model (DOM), CSS and XML.

Ajax has gained considerable popularity in the past few years, although the technologies that comprise the concept have been around for much longer than that. The term “Ajax” is attributed to Jesse James Garrett. I highly recommend his article Ajax: A New Approach to Web Applications

JavaScript selectedIndex property

Posted in JavaScript, Software, Web, internet, programming on May 24, 2007 by Joey

The selectedIndex property of a dropdown select list can be accessed via JavaScript as follows:

select name="SelectBox" onChange="alert(this.selectedIndex);"

The preceding example shows that selectedIndex is a property of the dropdown select list. selectedIndex is 0-based, meaning that the first item in the select list has a selectedIndex of 0; the second item in the select list has a selectedIndex of 1; and so forth. If nothing is selected, the selectedIndex is -1.

Related articles:
JavaScript: How to Get Selected Item in Dropdown Select List
Accessing Form Elements with JavaScript

Sun Names New Language JavaFX Script – Is this some sick joke?

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

As if it weren’t confusing enough explaining the difference between Java and JavaScript (which are unrelated), Sun Microsystems has muddied the waters further with the introduction of JavaFX Script. To quote from Sun’s website: “[JavaFX Script is] a highly productive scripting language that enables content developers to leverage the enormous popularity of Java to create rich applications and services for deployment on the widest range of platforms.”

OK, so that is fine that Sun has come up with this new programming language to use Java to build Rich Internet Applications. But to name it JavaFX Script? They seriously have to be kidding. This has to be the worst possible name that could have given to the language. Developers everywhere will now have the joy of having conversations such as this:

Manager: ‘You can code this interface in Java.’
Developer: ‘You mean JavaScript?’
Manager: ‘They are the same, right?’
Developer: [Explains difference between JavaScript and Java]
Manager: ‘OK, then go ahead and use JavaFX Script’
Developer: ‘You mean JavaScript?’
…grrrrrrrrr

Related Articles:
JavaScript Versus Java

JavaScript Versus Java: Differences and Similarities

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

What is the difference between JavaScript and Java? Actually, there is not much point in even discussing the similarities and differences because JavaScript and Java are unrelated languages.

JavaScript received it’s name from Netscape, who had originally developed the language using the name LiveScript, and changed its name at the last minute as a marketing move. Due to its name, it is often confused with Java, with many people thinking it’s a more simple form of Java.

JavaScript Switch Case Statement Example

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

The switch statement is used in JavaScript to evaluate cases for a variable and is used as follows:

var intTest = 1;
switch(intTest)
{
case 0:
document.write('0');
break;
case 1:
document.write('1');
break;
default:
document.write('unknown');
break;
}

The break statement exits the switch. If it is not used, subsequent cases will execute if also valid. For example, if no break was used in the case of 1 in the example above, both 1 and unknown would print.

The switch statement is preferable to multiple if statements as the tested value doesn’t have to be evaluated repeatedly.

String datatype example:

var strTest = 'a';
switch(strTest)
{
case 'a':
document.write('a found');
break;
case 'b':
document.write('b found');
break;
default:
document.write('unknown');
break;
}

JavaScript Arrays Summary and Reference

Posted in JavaScript, Software, Web, internet, programming on May 19, 2007 by Joey

JavaScript: Open a New Browser Window

Posted in JavaScript, Software, Web, internet, programming on May 19, 2007 by Joey

The JavaScript window object contains the open method. This can be used to open new browser windows via JavaScript, as follows:

window.open('url','windowname','options');

url is the path to the file to be opened in the new window
windowname is a variable reference to the opened window
options are the specifications of the opened window. The available options are:

  • height: The height of the window, in pixels
  • width: The width of the window, in pixels
  • top: The Y coordinate of the window, measured in pixels from the top of the screen
  • left: The X coordinate of the window, measured in pixels from the left of the screen
  • scrollbars: Whether to show horizontal and vertical scrollbars, if the page overflows
  • toolbar: Whether to show the browser toolbar
  • status: Whether to show the browser status line
  • menubar: Whether to show the browser menu bar
  • location: Whether to show the URL input field in the browser window
  • resizable: Whether the window can be resized

Example:

window.open('myFile.html','myWindow','top:40,left:100,menubar=no,toolbar=yes');

Note that options are separated by commas. If no options are specified, then all options are present and the windows is placed in the upper left corner of the screen.

JavaScript shift Method: Removing from an Array Beginning

Posted in JavaScript, Software, Web, internet, programming on May 18, 2007 by Joey

The JavaScript shift Method can be used to remove elements from the beginning of an array.

//Removes 1 from the beginning of the array
var aryTest = [1,2,3,4];
aryTest.shift();
document.write(aryTest);

Related Articles:
JavaScript unshift Method: Adding to an Array Beginning
JavaScript push Method: Adding to an Array End
JavaScript pop Method: Removing from an Array End

JavaScript Array For Loop Example

Posted in JavaScript, Software, Web, internet, programming on May 17, 2007 by Joey

The following code example demonstrates how to loop over a JavaScript array:

var aryTest = [1,2,3,4];
for (i=0;i<aryTest.length;i++)
document.write(aryTest[i]);

The length property of the JavaScript Array object is used to determine how many elements are in the array. These elements are then looped over, one-by-one, until all elements have been used, at which point the loop stops.

Related articles:
JavaScript Arrays Summary and Reference

JavaScript Array Length Property
JavaScript For Loop Example

JavaScript: Sort a List of Numbers

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

The following code can be used to sort a list of numbers in JavaScript:

function SortNumbers(a,b)
{
return a-b;
}
var lstNumbers = '20,25,5,15,10';
var aryNumbers = lstNumbers.split(',');
document.write(aryNumbers.sort(SortNumbers));

Related articles:
JavaScript Array Sort Method