A Story of Safari, Form, and OnSubmit

I learned something today.

I learned that, on Safari(iPod or mac… didn’t try PC), if one is using an <input type=”submit”/> in order to make the Enter key work when typing into a textbox (as I was doing in Hammurabi), the wrapping <form> tag will actually submit the contents of the form to the same URL that you are already at.  Firefox doesn’t do this.

In order to handle this correctly, you have to put an onsubmit handler that returns false on the form tag, or you will be feeding 2000 grain to your people, and suddenly you will have reset the game entirely.

Also, in the course of doing HTW and Hammurabi, I have determined a really nice way to structure the javascript for doing it.

Generally speaking, I use a single <div> that is the (for lack of a better term) “render target”.  Each time I update the content, I basically just replace the innerHTML property of the <div>.  This works rather well.  The game state will be held in some global javascript variable. The various event handlers of the <button> or <input> tags call other functions that further manipulate the state of the game.

This is all great, except that I determined that I needed to be able to start the content in one function, and be able to pass it to another function.  So, I came up with the following template for all of my text based game javascript functions:

function template(content){
    if(content==null) content="";
    //add content here
    document.getElementById("content").innerHTML = content;
}

This way, since all parameters of all javascript functions are optional, I can call something with no parameters and have the default value of null, which starts the content off as a blank string.

This way, I can have one function start off the content of another function with some sort of snide comment, or I can add some action resolution verbage to the status panel.

Advertisements

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: