Content storage techniques for dynamic HTML

Every now and then, you’ll be faced with a situation where you need to dynamically update HTML content.  One situation where this may occur is if you have an HTML form whose fields change based on specific selections.  Another situation would be updating the content of a dynamic modal window. But where do you store this dynamic content?  This article explains three techniques; each one with its own pros and cons.

1.  <textarea> method

You can store a block of HTML inside a <textarea> and set it’s display CSS attribute to none:

<html>
    <head>
        <script type="text/javascript">
            window.onload = function() {
                $('divContainer').innerHTML = $('txtContent').value;
            };

            function $(el) {
                return document.getElementById(el);
            }
        </script>
    </head>
    <body>
        <div id="divContainer"></div>
        <textarea id="txtContent" style="display: none;">
            <h1>
                Hello World!
            </h1>
        </textarea>
    </body>
</html>

pros:

  • Easy to read and edit content, as there are no special characters required.

cons:

  • The content of your <textarea> will be read by search engines as text, and will seriously impact the keywords considered important on your site.
  • This method will not work if your dynamic content contains a textarea.  You will run into an issue of having nested <textarea> elements, which will break functionality.

2. JavaScript array method

Content can be stored inside a JavaScript array.  The array can then be joined into a string:

<html>
    <head>
        <script type="text/javascript">
            var content = [
                "<h1>",
                    "Hello World!",
                "</h1>"
            ];
            content = content.join('');

            window.onload = function() {
                $('divContainer').innerHTML = content;
            };

            function $(el) {
                return document.getElementById(el);
            }
        </script>
    </head>
    <body>
        <div id="divContainer"></div>
    </body>
</html>

pros:

  • Clean formatting and fairly easy to work with.

cons:

  • Requires each ‘line’ to have quotes around it, with a comma at the end of each line.
  • Requires the javascript join() method to be run.  While this is a native method and is quite fast, it is still extra processing which needs to be done.

3. Multi-line JavaScript string method

Content can be stored as a multi-line JavaScript string:

<html>
    <head>
        <script type="text/javascript">
            var content = " \
                <h1> \
                    Hello World! \
                </h1>";

            window.onload = function() {
                $('divContainer').innerHTML = content;
            };

            function $(el) {
                return document.getElementById(el);
            }
        </script>
    </head>
    <body>
        <div id="divContainer"></div>
    </body>
</html>

pros:

  • Clean formatting and easy to work with.

cons:

  • If you are planning on minifying your code, all whitespace from indentations will be preserved.  This will impact how well your code can be compressed.

I usually use the third option.  It is easy to maintain large blocks of content, and keeps content out of your HTML, thus ensuring that search engines aren’t reading your HTML content as if it were text.

Amazon Wish Lists for your Shopify Store

Amazon Wishlist

With the holidays fast approaching, we’re eager to find ways to increase sales at The Ghostly Store. One great way of doing this is allowing customers to create sharable wish-lists of items from our shop.

The Shopify App Store doesn’t offer a satisfactory answer (market opportunity?), so we’ve opted for the next best thing: Amazon Wish Lists.

This is an indirect solution, as customers must also be Amazon members, but it’s a great execution, and links viewers of a wish-list right to our store. It’s worth noting that most online shoppers are already Amazon customers, so the membership hurdle won’t be an issue for many buyers.

The code is actually quite simple, so I’ve included it here for those who would like to try it for themselves.

1. The first step is to create a snippet in your store theme editor called “amazon-wishlist.liquid” with the following code.

<div id="wishlist">
	<div id="AUWLBkURL" style="display: none;">{{shop.url}}{{ product.url }}</div>
	<div id="AUWLBkTitle" style="display: none;">{{ product.title }}</div>
	<div style="display: none;" id="AUWLBkPrice">
	 {% if product.price_varies %}
	    between {{ product.price_min | money }} to {{ product.price_max | money }}
	 {% else %}
	   {{ product.price_min | money }}
	 {% endif %}
	</div>

	<div id="AUWLBkImage" style="display: none;">
		{% for image in product.images %}
			{{ image | product_img_url: 'large'}}
		{% endfor %}
	</div>

	<script src="http://www.amazon.com/wishlist/bookmarklet/getbutton.js?image=2" type="text/javascript" language="JavaScript1.2" id="AddToAUWLButton"></script>
</div>

2. Next, on each product template you have, include the following snippet into your page to render the Amazon button:


	{% include 'amazon-wishlist' %}

3. Now visit your store page to test it out. Here’s what it looks like for us:

Amazon Wishlist

Here’s the documentation and script if you want to customize this yourself at Amazon.com

PatternSketch: HTML5 and Javascript Audio Sequencer

We’re big music fans, and one of our favorite pastimes since our school days has been experimenting with music production software. Our first taste came in ’97 when Propellerheads released the ReBirth RB-338.

Since then, we’ve followed along and have been truly inspired by the development teams at Propellerheads, Ableton, and Image-Line.

A Browser Based Approach, Without Flash

Over the past few years, we’ve seen some really cool projects built with Flash that bring the music creating experience to the browser. Two of our favorites being Aviary and Audio Tool.

But, we’re not Flash developers, and with HTML5′s emerging promise of native functionality (no longer forcing us to rely on third party plugins),  we thought, why not give this a shot?

So, we’re happy to introduce PatternSketch, our browser based Drum Machine and Sequencer.




Features: Saving, Collaborating, and Exporting

In addition to the advertised feature-set of using your keyboard to tap out rhythms, or creating loops, we tried to focus our attention on ways to make it something actually useful. Once the initial sequencing features were built, a lot of our conversation surrounded the idea of collaboration amongst friends.

Since we’re using JSON to record patterns, we decided to save this information to a database so you could call it up at a later date. Saving patterns opened the door to sharing patterns, allowing friends to modify your creation, and send back to you.

Another idea was around portability. Since your limited within this context to only producing rhythms (no chords, bass, melody, etc), it made sense to create functionality to export your loops.  This allows users to ‘sketch’ a pattern online, then continue working on the project with more sophisticated audio hardware or software offline.

Performance Issues

Despite what you can do with JavaScript, it isn’t currently possible to execute accurate timing, due in large part to JS’s single-threaded nature.  This is unfortunate, because the human ear can detect a timing miscalculation as small as a few milliseconds.

Until this issue can be resolved, we’re aware the limitations for practical use exist.

Regardless, we think PatternSketch is an exciting project and preview of audio tools we may see in the near future, and we’re delighted to share it with everyone.

Shout Outs

A special thanks to Doug Koh, who provided us with an awesome design, and the name ‘PatternSketch’.

We’d also like to thank the developers out there who’ve tried this already and gave us further inspiration, including but not limited to Bryan Arnold, Stephen Eisenhauer, and Jesse Jackson.

Finally, we’d like to thank Ghostly International for hosting the Visual Masterclass at Eyebeam, and the participants who gave feedback and encouragement during the birth of the project.

Patternsketch »


Bringing MoodStats Online

I used to use a handy app to monitor the trends in my mood called MoodStats, developed by the team at Cuban Council.

Maybe it’s related the changing of the seasons, but around this time of year I tend to become a little more aware of my mood, and it’s fluctuations. And I find value in being able to track and review these trends.

The beauty of MoodsStats was the simplicity and speed at which you could record your data. And of course, the colorful graphs it made to review it.

Unfortunately, a web version of this app was never created, and inability to use on multiple machines has always been a major shortcoming of the system for me.

I’ve waited for years for Cuban Council to bring this to the browser, especially since the data synching functionality seemed to make it just a few steps away from reality, but it seems the project has since been abandoned.

So, my fall project is going to be a simple port of MoodStats to the web.

To keep this within the scope of a night and weekend project, and to not drag it on for months, I’ll keep the feature-set light to start, and iterate over the base if I’m in the mood ;)

The most important factor is keeping the mood entry interface clean and fast.

If you work at Cuban Council and you want me to stop, or are interested in collaboration, please reach out (miguel(at)ghostly dot com).

Stay tuned.