May 30, 2002

Fun with JavaScript

When sitting around with extra time on my hands, there's nothing like a online game for entertainment. I found this interesting JavaScript version of Battleship and decided to fancy it up a bit.

For starters, I practiced with some "onClick" code for the button to pop up a window in which to play the game in. The original game displayed your opponent's ships in the window status bar. That didn't suit since I was using a popup window. Snooping through the code I found the function that passed the status value (a variable named "statusmsg") and added one additional line: document.statusbox.display.value = statusmsg; (you gotta love DOM, eh?). The final touches were adding a form to the page (named "statusbox") and a readonly textarea (of course, named "display") and a handy little button that would call a function to reload the page when the game is over.

Credits for the original JavaScript are in the source code. It makes for good reading.

Addendum: After thinking about it for awhile, I realized that I didn't need a special function just to reload the game. Again, the DOM comes in handy and I simply used the reload method of the "location" object. Gee, isn't that easier?

So, give it a try: