Introducing JavaScript

JavaScript has changed a great deal since it was first introduced but many sites still use outdated techniques. There are even lots of JavaScript tutorials on the web that show you how JavaScript should be written so that it can work in Netscape 2 rather than so that it will work in modern browsers. As a result there are many people who are writing what they think is JavaScript but where what they are producing is more suited to the 20th Century than to the 21st.

Beginners should work through all the basic examples prior to moving on to the other examples which cover separate aspects of JavaScript in more detail.

Most of the JavaScript examples here will work in IE5+, Opera7+ and all versions of Firefox, Safari and Chrome. Those commands introduced in ECMAScript 5 in 2011 are being added to the site where they are supported by the latest version of all popular browsers. Where these new commands are not supported by IE8 or IE7 that information is included in the text since those two browsers might still have to many users for you to ignore. So few people using other browsers fail to keep their browser up to date that lack of support in older versions of other browsers will not have any significant affect.

A few examples of what we can expect from EcmaScript 6 are also included in their own section so you can see the direction that JavaScript is moving in - even though it will probably be quite a while before we can use those commands.

There is a link to jsBin at the bottom right of all the pages. This site provides an easy way to test your JavaScript code online without needing to create an entire web page to run a simple test.

Different Animations

We can apply different animations to each element when we have multiple animations on the same page. Here we have one element following a predefined path and a second using our earlier variant of selecting end points at random.

Multiple Animations

Having one element move around on our web page is a start but what do we need to do if we want to have more than one element moving around at the same time? Well animating multiple elements doesn’t require much more code than animating the first one did.

Follow A Path

Rather than having each end point selected at random, let’s look at how we can predefine a series of end points so that our element follows a predefined path around the browser viewport.

Perpetual Motion

Now that we know how to get our element moving in a straight line in any direction, let’s next have it choose a new random end point whenever it reaches the previously selected end point so that the element continues to move indefinitely in a succession of randomly chosen straight lines.

Random Movement

Progressing toward more complex movement of an element on our web page, let’s next look at how we can allow the element to move in a straight line in any random direction.

This site is © copyright Stephen Chapman - Felgall Pty Ltd 2011-2014.

Privacy Policy | Terms and Conditions

You are welcome to use any the example JavaScript from this site in the scripts for your site or any that you develop for others but may not use the longer example scripts that contain a copyright notice in any other way without permission.