JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript

Hangman Script

1. Introduction and Body Script

clr gif

Anyone want to play a game of hangman? Whether you want to test your visitors for particular knowledge or just provide a bit of fun you can easily put a hangman game onto your site using this script maker.

Although the game is just for fun, it spoils the fun somewhat if it is possible to cheat. You will notice when you generate the head script containing your answers that the information appears to bear little relationship to the ansers you type in. Never fear the script knows what your answers are and someone would have to spend a significant time decoding all of the answers in order to tell just which ones are possibilities.

The whole game is made up of two javascripts (one that goes in the head of the page and one that goes in the body of the page), a stylesheet, and eleven images. If you are interested in how the script works then read the hangman script tutorial otherwise just continue on and add the script to your web page by following the instructions that follow.

Note that there are two versions of the body script for this game. The one below uses eleven images for the drawing of the "hangman" that appears in the middle of the game. If you prefer a version that doesn't require images then you need to use the alternative body script instead of the one below.

We will start by highlighting and copying the below Javascript and then save it as a file called hangb.js. There is a button below the form to make highlighting the code easier.

You then link this code into the body of your page where you want the script to appear using the following code:

<script src="hangb.js" type="text/javascript">
</script><noscript><div align="center"><b>The Hangman
game requires Javascript</b><br />You either have
Javascript disabled<br />or the browser you are using does
not<br />support Javascript. Please use a Javascript
<br />enabled browser to access this game.</div></noscript>

The next step is to set up a selection of answers that will go into a script in the head of the page.

To see how scripts created with this generator actually work take a look at my Javascript Objects Hangman.

Explore JavaScript

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.