Click here to Skip to main content
15,881,938 members
Articles / Web Development / HTML

MiniRacer - Extending W3C DOM Elements using JavaScript (IE)

Rate me:
Please Sign up or sign in to vote.
4.13/5 (5 votes)
16 Feb 20058 min read 83.1K   499   22  
A JavaScript table-based driving game to demonstrate how to extend W3C DOM Elements to add your own functionality.
var tbl;	// stores a reference to the RaceTrack table

function setup(boolIgnoreQueryString)
{
	// if boolIgnoreQueryString is true then make a blank racetrack from
	// the size indicated in the two select elements on the web-page
	// otherwise load racetrack in from querystring

	tbl = document.createElement("RaceTrack");
	
	var qs = new QuerystringParser();
	var track = qs.getRaceTrack("NA");
	var rowLength = qs.getRowLength("NA");

	if(track=="NA" || rowLength=="NA" || boolIgnoreQueryString)
	{
		// get choice from drop down list
	 	var rows = document.getElementById("rows").value;
	 	var cols = document.getElementById("cols").value;

		tbl.makeTrack(rows, cols, ENABLE_CELL_CLICK_HANDLER);
	}
	else {
		tbl.load(track, rowLength, ENABLE_CELL_CLICK_HANDLER);
	}

	// add racetrack to webpage
	var container = document.getElementById("RaceTrackContainer");
	container.replaceChild(tbl, container.firstChild);
}

function startRace()
{
	if(!tbl.hasStart() || !tbl.hasFinish())
	{
		alert("Your track does not have a start and a finish point!");
		return;
	}

	// re-direct to racing page
	window.location = "miniRacer.htm?track=" + tbl.serialise() + "&rowLength=" + tbl.getRowLength();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
Teacher: Maths and Computing, Secondary School.

First learnt to program in BASIC on an Amstrad CPC464 and then continued messing around with programs at university and ever since. Doesn't know a huge deal about any of it really but enjoys the challenge and is always keen to learn a new trick!

Comments and Discussions