JavaScript topics

Dec 5 06

Waiting for the DOM to Load

As we have started to learn about the DOM this season, one thing that is an issue when developing your scripts is knowing when you can have your scripts execute. Remember, the DOM is available to you only when it has finished loading in the browser. You may ask, when has it finished loading? Good Question!

The order of completion that takes place inside a browser is roughly:

1. HTML is parsed
2. External scripts/style sheets are loaded
3. Scripts are executed as they are parsed in the document
4. DOM is fully constructed
5. Images and external content are loaded
6. The page is finished loading

read more…

Nov 27 06

Google Maps, Yahoo! Maps, Virtual Earth, MapQuest

Updated September 2009
The mapping APIs of these four have greatly changed since this post and I will be re-reviewing these in a future post. Code examples were removed.

Since the emergence of Google Maps giving us the ability to put an interactive map on your own web page(s), there are now four separate APIs to choose from: Google Maps, Yahoo! Maps, Microsoft Virtual Earth Bing Maps, and MapQuest. These APIs are similar, so once you understand one; it is just a matter of learning the differences of the others.

It’s pretty interesting noticing the visual and functional differences as you go back and forth looking at each one. Personally, I favor Google Maps because of its smooth interface, clean look, and easy to understand documentation, but Yahoo! Maps is a close second.

Yahoo! Maps also provides a Flash API version (see below) that can use either JavaScript or Flash ActionScript code. 

Virtual Earth reminds me of the interfaces Microsoft has for many of its development products (which can be good or bad), but provides the most intriguing visual experience with its aerial and bird’s eye views. MapQuest is the new kid on the block and has the least visually entertaining experience, but incorporates its biggest strength with driving directions so it has some potential.

All of the APIs are built upon the theory of geocoding, but offer different implementations for allowing you to retrieve latitude/longitude points either within the JavaScript API or using external REST calls. For the later, you will need to incorporate some server-side scripting (PHP, ASP, etc) to retrieve the information you want. If you do not want to be bothered with that, but need specific points (latitude/longitude) for an address(s) you can use websites such as http://geocoder.us/ or you can use my simple geocoding example.

There actually is another mapping API called Map TP, but requires that the browser have the Java Runtime installed.

Nov 8 06

Want a Cookie?

Cookies are really the only way in which you can share user data between two non-sequential web pages. Cookies allow you to store data on users’ systems and retrieve it in minutes, hours or even days later. This makes cookies perfect for remembering things like users’ login names, which pages they visited last, what they dropped into their shopping carts and when they did so.

Although server-side code is the most robust method of dealing with cookies, sometimes you need to manipulate cookies on the client side. Luckily, JavaScript can read and write cookies in exactly the same way servers do. Though the problem with JavaScript is that reading a cookie requires a little parsing to get them out. Don’t worry, I found a nice little function to the job for us.

read more…

Nov 7 06

Using Timers in JavaScript

Timers are a way to add a dynamic aspect to your web pages. They can be used to create animations, open or close windows, pop up a message to the user, and even destroy a cookie for security purposes.

There are two types of timers: one that’s set once, and one that reoccurs over an interval. Both can be canceled, though the one-time timer method fires just once.

read more…

Nov 1 06

Is Random Really Random?

As random as random can be.

A computer cannot generate a truly random number because computation is deterministic: it follows an unbroken chain of cause and effect in which no truly random events ever occur. Instead, it uses a set of complex algorithms to generate what’s known as a pseudorandom number — a number that gives the appearance of randomness, and is good enough for any practical purpose.

read more…

Aug 11 06

JavaScript Functions 101

A function is a self-contained block of statements. Functions are very good at holding reusable code. You can declare a function by using the function keyword followed by the name of the function you want to create:

function myfunction() {
     // do something
}

You execute the function like this:

myfunction();

read more…

Aug 5 06

History of JavaScript

In an issue of Treehouse magazine, Jeremy Keith wrote an excellent article (.pdf) about the history of JavaScript. He explained the beginnings of JavaScript, the emergence of DOM scripting and the importance of standards-based scripting focsing on the separation of layers.

Apr 30 06

Flash Embedding and Detection

Earlier this month, Microsoft announced a security update to Internet Explorer that changes the way it handles web pages that contain ActiveX controls. This would affect any control (Flash, video plug-in, etc.) that uses the APPLET, EMBED, or OBJECT tags. Basically the control’s user interface is blocked until the user activates it by clicking on it or hitting the space bar when it has focus.

read more…