4

I've been playing around with the idea of creating a javascript version of the original Colossal Caves Adventure, as a way to teach myself the language. The original data file for the adventure can be found here -

http://jerz.setonhill.edu/if/crowther/

I'm hoping an experienced javascripter can suggest to me the best way to store and access the text data file (which is only a few hundred lines long). At the moment I'm thinking of embedding the text in a hidden element in the html page and accessing it that way, but I know that lacks elegance. What are your thoughts?

Textmode
  • 509
  • 3
  • 18
Craig Schwarze
  • 11,367
  • 15
  • 60
  • 80

2 Answers2

2

You could dive in the deep end and learn AJAX while you're at it. Grabbing content as needed is exactly what AJAX is great at. (If you're learning JS, you will eventually learn AJAX)

If you really want to load all content on page land, I would recommend breaking the content into individual <input type="hidden"s. JavaScript grabs their contents very easily, and you can give them descriptive ids that will make serving the content more intuitive during programming. e.g.

<input type="hidden" name="r121" value="You come upon a fissure" />

Which could mean result r 121 is "You come upon a fissure"

Shad
  • 15,134
  • 2
  • 22
  • 34
0

Create a javascript object assigned to a variable and save this piece of code as a separate script. Keeps the data and the control code separate.

So, maybe you have a file called advdat.js that contains this:

advdat = {  1: "YOU ARE STANDING AT THE END OF A ROAD BEFORE A SMALL BRICK\
                BUILDING . AROUND YOU IS A FOREST. A SMALL\
                STREAM FLOWS OUT OF THE BUILDING AND DOWN A GULLY.",
            2: "YOU HAVE WALKED UP A HILL, STILL IN THE FOREST\
                THE ROAD NOW SLOPES BACK DOWN THE OTHER SIDE OF THE HILL.\
                THERE IS A BUILDING IN THE DISTANCE.",
            3: "YOU ARE INSIDE A BUILDING, A WELL HOUSE FOR A LARGE SPRING.",...
          };

Load this in its own script tag before loading the main program. Your data is now available in the object advdat.

Jerry Neumann
  • 415
  • 1
  • 4
  • 11