Archive for November, 2005

AJAX in Pocket IE?

Wednesday, November 30th, 2005

According to the IEMobile Team Weblog, the answer is Yes! [Thanks Ajaxian Ben & Dion]

They have some sample code and an example page. Also, they point out:

  1. On Smartphone/PocketPC 2003
    • innerText and innerHTML Properties are only supported on div and span elements
    • Form elements are scriptable as well
  2. On Windows Mobile 5
    • innerText and innerHTML Properties are supported on all elements
    • In addition there is support for document.all and the style object

I had noted some limitations of Pocket IE DOM manipulation in a previous post.

How Should We Then Live?

Tuesday, November 29th, 2005

Steve Pavlina:

By their words I hear that most Americans are Christian. By their actions I see that most aren’t.

Why [Not] Ruby?

Monday, November 28th, 2005

I’ve recently been playing around with Ruby (yes, and Rails).

Though there are still some language issues that need to be sorted out (like variable scoping), Ruby seems to be a very elegant language, with an intuitive syntax (and by intuitive I don’t mean to imply similarity to C/C++/Java). I particularly enjoy it’s poetic terseness: Ruby code is actually readable (for the most part, anyway).

FWIW: I don’t think Ruby will ever be a 100% replacement for Java — and I hope the priests of the Java cathedral are extremely circumspect when it comes to attempts to push Ruby-like features into the language (we could end up with something even more disastrous than Generics!).

The Truth about Multitasking

Wednesday, November 23rd, 2005

JavaScript Dynamic Content Templates

Wednesday, November 23rd, 2005

For a recent project, we had to do some dynamic content replacement on a web page. We had to support a primitive version of IE (viz., Pocket IE for Windows Mobile 2003), so fancy DOM element creation and manipulation were out of the question — we were stuck with simply changing the value of the exposed innerHTML JavaScript attribute of <div>s, <span>s, etc.
Some of the dynamic content was relatively long (using tables, etc for layout), so rather than creating it using a long series of JavaScript string concatenations (with all necessary character escapes), we created <span>s that contained template HTML. For example:

<span id="template" style="display:none;">
<table>
 <tr><th>Name</th><th>Value</th></tr>
 <tr><td>{propertyName}</td><td>{propertyValue}</td></tr>
</table>
</span>

Then, we can get the template string:

var templateStr = template.innerHTML;

Notice that the template has a couple strings enclosed in curly braces. These are tokens that will be replaced by values from a JavaScript object. A simple JavaScript method takes care of this:

<script language="JavaScript">
function doTemplateReplacements (template, tokensObj)
{
var output = template;
for (var prop in tokensObj)
{
  var re = new RegExp ("\{" + prop + "\}", "g");
  output = output.replace (re, tokensObj[prop].toString());
}
return output;
}
</script>

Usage is pretty straightforward. Simply pass the template string and an object with properties matching the tokens in the template. Then, set the innerHTML of your “target” container to the dynamicContent string.

<span id="target"></span>

<script language="JavaScript">
var dynamicContent =
   doTemplateReplacements (templateStr,
                           { propertyName: "Cost",
                             propertyValue: "$100.00" });
target.innerHTML = dynamicContent;
</script>

The target <span>’s content becomes

<table>
<tr><th>Name</th><th>Value</th></tr>
<tr><td>Cost</td><td>$100.00</td></tr>
</table>

A while after devising this approach, I ran across the TrimPath JavaScript Templates library. This library supports this simple templating and a *lot* more. I haven’t actually used this library, but it’s interesting in that it implements a micro expression language for use in the templates. Might have been useful in the project, but I’m not sure if it would be 100% functional in Pocket IE.