Articles

What is Javascript?

So, continuing from my last post “What is HTML?”, I mentioned that from pure HTML, you can display and format text and images, and you can link pages together, but it doesn’t account for a lot of the whizzy exciting things that web pages can do. Well, the next step is Javascript.

Javascript is not anything to do with the Programming language Java, which is used in Android Mobile phones, and countless other devices, although is becoming somewhat rarer when used in Web Pages. Javascript is a programming language, which gets embedded into web pages adding functionality, and allowing the pages to be modified on the fly without having to be reloaded from the web server. This is what does the brunt of the work for web pages these days, and it’s use and prevalence is what has led to it being the most commonly used programming language in the world at this point.

While going into the details of how Javascript works is far beyond the scope for a brief blog posting, what makes it most interesting and useful is what is called Event Handlers. Quite simply the handlers are connected to things the user does, and it runs Javascript code on occurrences of these Events.

So, the main Event Handlers are onclick, onmouseover, onmouseout, onkeydown, onload and onunload.

onload, runs code when the page is loaded, so anything you want to happen as soon as the page is seen by the user.

onmouseover, runs code when the user moves their mouse pointer over and element on the screen, so making an image larger when they are about to click on it, changing the colour of a word when the user moves their pointer over it, etc.

onclick, runs code when the user clicks on an element, so for example anything on the screen can be made into a link, or adding up prices to give a total when the user click subtotal on a shopping screen,etc.

on mouseout, runs code only when the user moves their pointer off an element on the screen, so changing the element back to it’s original format after onmouseover has changed it.

I’ll leave you to work out what onkeydown does (this programming thing is easy really isn’t it).

A really interesting one is onunload, which is run when you move away from a page or close it, used for “Are you sure?” type messages.

As a programming language, Javascript operates in mainly the same way as all languages to, a simple if – then sequence. If something happens or some equation equals a result, then do something. So in a game, if the player gets hit by a bullet, then they lose some health, if their health equals zero, then they die.

This basic concept is the ground work for all computer program, and is the core of computer decision making, breaking larger problems down into small if then statements, even typing on a page, if the user hits the q key, then display a letter q.

There are some modifications to this, for example the else statement, so again in a game, if the user tries to shoot, if they have bullets left, then let them shoot and reduce their number of bullets, elseĀ make a clicking noise to let them know they’re out of bullets. But while this saves time, it is just a variation on a theme (for example, in the above the same effect could be reached by an if statement saying if bullets equals zero, then play clicking noise). But the core of every program is the basic if then decision process.

Now, Javascript gains a lot of its power from interaction with something called CSS, which defines the exact layout of a page, but from the event handlers listed above, and knowing how computers make decisions, I’m sure you’ll see how Javascript is used on some of the web pages you visit, and can begin to see interesting ways it can be used to really interesting things with web pages.

blue-network-sphere

Leave a Reply

Your email address will not be published. Required fields are marked *