<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
    <title>the ender.com blog</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/" />
    
    <id>tag:www.ender.com,2008-04-03://2</id>
    <updated>2010-09-24T21:00:55Z</updated>
    <subtitle>lamp, ajax, linux, iphone, and other fun things</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Open Source 4.1</generator>

<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/LastBlogStandingtheEndercomBlog" /><feedburner:info uri="lastblogstandingtheendercomblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site, subject to copyright and fair use.</feedburner:browserFriendly><entry>
    <title>The Wrong Way to Ajax</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2010/09/the-wrong-way-to-ajax.html" />
    <id>tag:www.ender.com,2010://2.195</id>

    <published>2010-09-24T20:31:06Z</published>
    <updated>2010-09-24T21:00:55Z</updated>

    <summary>Recently I logged onto LinkedIn, to belatedly answer to connection requests. LinkedIn now has some fancy ajax stuff, so you can just click accept on an invitation, and instead of loading a page, it does a background request and, if...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
        <category term="AJAX" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Javascript" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="jQuery" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>Recently I logged onto <a href='http://www.linkedin.com'>LinkedIn</a>, to belatedly answer to connection requests.</p>

<p>LinkedIn now has some fancy ajax stuff, so you can just click accept on an invitation, and instead of loading a page, it does a background request and, if all goes well, that invite just disappears with a success message, leaving you to deal with the next request without leaving the page.</p>

<p>That's great. I did a lot of that when doing web application work for <a href='http://www.daz3d.com'>DAZ3D</a>. One of the hallmarks of efficient e-commerce is giving people tools to get things done faster - and that meant things like list filters, type-ahead searches, ajax'd add-to-cart buttons - you name it.</p>

<p>But LinkedIn has an issue. See the screenshot:</p>

<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.ender.com/images/linkedin_err.html" onclick="window.open('http://www.ender.com/images/linkedin_err.html','popup','width=1233,height=967,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img border='0' src='http://www.ender.com/images/linkedin_err.png' width='500' /></a></span></p>

<p>I've used <a href='http://getfirebug.com/whatisfirebug'>Firebug</a> to peek at what is happening when it makes the request. As you can see, there's an error. The first time, I wrote it off as something transient, but I came back days later and got the same error.</p>

<p>I decided to log off and log back on, and the issue was fixed. Clearly, the "remember me" was sufficient for a normal page view, but not sufficient for accepting a connection request.</p>

<p>The problem arises because the ajax request is getting a normal full pageview back with the 500 error. I've written before about <a href='http://www.ender.com/2008/04/jquery-the-jquery-form-plugin.html'>detecting ajax</a>; if you're going to send a request over ajax, then whatever is handling the request should be aware, even if it's an error. There's nothing wrong with a 500 code for an ajax request that errors out; but your javascript should be ready to handle it.</p>

<p>In this case, as you can see from the (slightly privacy-screened) screenshot, I had a "spinny" (that's the technical term) that was going to sit there forever. That's especially bad, as a normal user will have no idea if the request has failed or is taking a really long time. Ideally, you throw a good error to the user; or, in this case, since you're not accepting their stale credentials, even redirect them to a login box. What you <b>don't</b> do is just throw a 500 error the javascript can't interpret and then stop there.</p>

<p>I personally like returning JSON objects, and I prefer to return them regardless of the code: 200 or 500, you get JSON if you made an ajax request. Then, my javascript always "fails over" - it looks for success. If there's no success message, it's an error.</p>

<p>Example:</p>

<blockquote class='code' style='font-size: 9px'>
$.get(href,
  function (data, textStatus) {
    var res = handleJSON(data);
    if (res['success']||false) {
      tgt.after("<span id='subsvcmsg' class='subsvcmsg success'>"+(res['success']||'Subscription added.')+"</span>");
      $("#subsvcmsg").attr("id", "").fadeOut(2500, function() { $(this).remove(); });
    } else {
      tgt.after("<span id='subsvcmsg' class='subsvcmsg error'>"+(res['error']||'An unknown error occurred trying to modify that subscription')+"</span>");
      $("#subsvcmsg").attr("id", "").fadeOut(2500, function() { $(this).remove(); });
    }
  }
);
</blockquote>

<p>As you can see here, this code tests for a success message - using javascript's || operator to avoid any error messages being thrown to the user even if that element doesn't exist - and unless a success message is available, it prints an error. It tries to use the error returned by the server, if one exists - but again, if the error isn't there, it has a default message.</p>

<p>The point here is: don't leave your users hanging. (Also, you may not want to tell people your crack team is springing into action to handle a 500 error if that's not the case and it will still be failing days later, but that's aside from the point.)</p>]]>
        
    </content>
</entry>

<entry>
    <title>MySQL Performance: # of columns vs varchars</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2009/10/mysql-performance-of-columns-v.html" />
    <id>tag:www.ender.com,2009://2.193</id>

    <published>2009-10-08T23:20:09Z</published>
    <updated>2009-10-08T23:35:31Z</updated>

    <summary>I have long told fellow developers that trying to stick to tables that have entirely fixed width columns is good practice. This tends to lead to, at the least, normalizing out text on a performance sensitive table/application. Instead of: CREATE...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>I have long told fellow developers that trying to stick to tables that have entirely fixed width columns is good practice.</p>

<p>This tends to lead to, at the least, normalizing out text on a performance sensitive table/application.</p>

<p>Instead of:</p>

<blockquote class='code'>
CREATE TABLE foo (
  idx int(10) unsigned not null auto_increment primary key,
  productId int(10) unsigned not null,
  price decimal(7,2) not null,
  description text not null default '',
  KEY productId_idx(productId)
);
</blockquote>

<p>It might be broken up into:</p>

<blockquote class='code'>
CREATE TABLE foo (
  myInt int(10) unsigned not null auto_increment primary key,
  productId int(10) unsigned not null,
  price decimal(7,2) not null,
  KEY productId_idx(productId)
);
CREATE TABLE foo_text (
  foo int(10) unsigned not null primary key,
  text not null default ''
);
</blockquote>

<p>The idea is that if you're doing most operations with the "foo" table, you'll get much better performance without a variable width column (varchar, text, etc).</p>

<p>They used a table with a varchar(1) column, and got a massive performance hit, but only when the number of columns was large. I'd like to experiment to see if it is the number or pure width of the columns. (In short, compare something like 99xchar(3)+1 varchar(1), or 1xchar(297) + 1 varchar(1).<br />
</p>]]>
        
    </content>
</entry>

<entry>
    <title>MySQL NULL gotcha; or, yes, Virginia, types do matter</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2009/04/mysql-null-gotcha-or-yes-virgi.html" />
    <id>tag:www.ender.com,2009://2.192</id>

    <published>2009-04-28T20:15:51Z</published>
    <updated>2009-04-28T20:23:04Z</updated>

    <summary>MySQL builtin comparison functions and NULLs</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>If you spend a lot of time dealing with languages that are loosely typed and have scalars running wild, then like me, you may be prone to doing something like this:</p>

<blockquote class='code'>
mysql> select NOW(); select GREATEST(NOW(), DATE_ADD(NOW(),INTERVAL 1 DAY));
+---------------------+
| NOW()               |
+---------------------+
| 2009-04-28 14:17:36 | 
+---------------------+
1 row in set (0.00 sec)

<p>+-------------------------------------------------+<br />
| GREATEST(NOW(), DATE_ADD(NOW(),INTERVAL 1 DAY)) |<br />
+-------------------------------------------------+<br />
| 2009-04-29 14:17:36                             | <br />
+-------------------------------------------------+<br />
1 row in set (0.00 sec)</p>

<p>mysql> select GREATEST(NULL, NOW());<br />
+-----------------------+<br />
| GREATEST(NULL, NOW()) |<br />
+-----------------------+<br />
| NULL                  | <br />
+-----------------------+<br />
1 row in set (0.00 sec)<br />
</blockquote></p>

<p>I'm glad the bug this caused had a minor impact, and this will hopefully be sufficient to slap me out of my loosely typed comfort zone for a while.<br />
</p>]]>
        
    </content>
</entry>

<entry>
    <title>Worth</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/10/worth.html" />
    <id>tag:www.ender.com,2008://2.171</id>

    <published>2008-10-23T05:41:57Z</published>
    <updated>2008-10-23T05:43:00Z</updated>

    <summary>This may be worth following....</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p><a href='http://neopythonic.blogspot.com/'>This</a> may be worth following.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Seinfeld is an appropriate Microsoft shill; Vista is a joke</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/08/seinfeld-is-an-appropriate-mic.html" />
    <id>tag:www.ender.com,2008://2.131</id>

    <published>2008-08-22T00:44:00Z</published>
    <updated>2008-08-22T01:52:07Z</updated>

    <summary>... Windows is the "Smelly Car" of personal computing ...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
        <category term="rants" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>I was pleased and amused at news that <a href='http://online.wsj.com/article/SB121928939429159525.html?mod=googlenews_wsj'>Seinfeld  will apparently pimp Vista</a>, which has been ravaged by issues such as <a href='http://www.tomshardware.com/reviews/xp-vs-vista,1531-11.html'>poor performance</a>, the way UAC <a href='http://msmvps.com/blogs/thenakedmvp/archive/2007/02/15/user-account-control-uac-on-vista-a-useless-feature-t.aspx'>trains users to click yes without providing real security</a>, to say nothing of how Vista <a href='http://www.electronista.com/articles/08/01/04/vista.drm.and.netflix/'>has horrible DRM</a>, which distrusts the user and <a href='http://arstechnica.com/articles/paedia/hardware/hdcp-vista.ars'>hurts consumers</a>.</p>

<p>Vista is so bad, Microsoft came up with this elaborate plan to <a href='http://www.betanews.com/article/Is_Microsofts_Mojave_Vista_experiment_backfiring_with_users/1217535233'>trick people into saying nice things about Vista</a>. It might be easy to trick a few novice users into thinking Vista looks neat when they're in a lab, but do they know about the performance issues it may have on their hardware, or the frustrating DRM? For that matter, can they navigate the small ocean of different Vista licenses to select the right one? Vista is not what you'd expect from a half-decade of development. When you spend billions upon billions of dollars on a new operating system, you'd expect more than a "<a href='http://defectivebydesign.org/'>Defective By Design</a>" piece of junk.</p>

<p>All this leads me to conclude that Seinfeld is the <b>perfect</b> spokesperson for Vista. Because Vista is a joke. Windows is the <a href='http://en.wikipedia.org/wiki/The_Smelly_Car'>Smelly Car</a> of personal computing. It makes computers stink, and Vista is just the latest horrible manifestation. </p>]]>
        
    </content>
</entry>

<entry>
    <title>If this is an iPhone killer, don't take out insurance yet</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/08/if-this-is-an-iphone-killer-do.html" />
    <id>tag:www.ender.com,2008://2.111</id>

    <published>2008-08-08T05:41:08Z</published>
    <updated>2008-08-08T05:46:45Z</updated>

    <summary>Seriously Wired, what's wrong with you? The E71 looks more like a Blackberry Killer, but don't be fooled: This great white hope gives the iPhone a run for its money in a lot of different areas (yes, really). No, not...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>Seriously Wired, <a href='http://www.wired.com/gadgets/gadgetreviews/multimedia/2008/08/gallery_gadgets?slide=8&slideView=7'>what's wrong with you?</a></p>

<blockquote>The E71 looks more like a Blackberry Killer, but don't be fooled: This great white hope gives the iPhone a run for its money in a lot of different areas (yes, really).</blockquote>

<p>No, <b>not really</b>.</p>

<ul>
<li>Big, nasty keyboard</li>
<li>Display not as large as ipod, let alone iphone</li>
<li>MicroSD, but only 8GB.</li>
<li>$500 unlocked. Ouch. You're not iPhone 3.0, you know.</li>
<li>No headphone jack.</li>
</ul>

<p>So it's an iPhone killer because... uh... the battery lasts 3 days!  Yeah... because you can't do anything cool with it.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Slacker</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/07/slacker.html" />
    <id>tag:www.ender.com,2008://2.106</id>

    <published>2008-07-15T04:18:24Z</published>
    <updated>2008-07-15T04:28:35Z</updated>

    <summary>I'm aware I haven't posted much. That said, I'm hoping to have an app I've been working on for the GAE (Google App Engine) done shortly, and will post some impressions about the GAE, and Python. (Wait, do I need...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>I'm aware I haven't posted much. That said, I'm hoping to have an app I've been working on for the GAE (Google App Engine) done shortly, and will post some impressions about the GAE, and Python. (Wait, do I need a new category?) </p>

<p>After that, now that Apple has opened the developer floodgates, it's time to put in some time with my Cocoa for MacOSX book, iphone dev docs, etc, and do some iphone work. I enjoy my own iphone way too much to not develop something for it. Yay open platform! I did a small app for PalmOS ages ago, never even distributed it, but just had to to do it because I loved my palm and there was a decent gcc-based toolchain.</p>

<p>I am planning to upgrade to 3g, despite battery concerns. If necessary, I will disable the 3g except when I expect to be using lots of data. I never had an issue with call quality, and don't talk enough (<100m a month on average) to care. Maybe I can figure out a way to make that hotkeyed in some way. (I'd like to have a handful of settings toggles available when you double-click home when locked, like the ipod controls)</p>

<p>I grabbed a <a href='http://svenontech.com/reviews/?p=74'>mybat external battery</a> for the iphone. We'll see how that works, but tucked in my laptop bag, hopefully that will be serviceable in the clutch when battery is low and a recharge opportunity is not coming.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Xen and the Art of Virtualization</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/06/xen-and-the-art-of-virtualizat.html" />
    <id>tag:www.ender.com,2008://2.103</id>

    <published>2008-06-20T00:10:44Z</published>
    <updated>2008-06-20T02:10:05Z</updated>

    <summary>I have historically had several servers at Layered Tech. They recently raised their prices, and the prices for several friends who have hosted servers there. The majority of our use is low volume, experimental sorts of things, and so we...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
        <category term="LAMP" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="linux" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>I have historically had several servers at <a href='http://www.layeredtech.com'>Layered Tech</a>. They recently raised their prices, and the prices for several friends who have hosted servers there. The majority of our use is low volume, experimental sorts of things, and so we decided to go in together on a larger box that we could virtualize.</p>

<p>Enter <a href='http://www.voxel.net'>Voxel</a>, who offer a $199/mo dedicated server that includes:</p>

<ul>
<li>4 GB RAM</li>
<li>Quad XEON 2Ghz</li>
<li>320GB SATA disk (which are growing more standard among these providers, but a lot of options are still IDE</li>
<li><b>SSH-accessible serial console access included</b></li>
</ul>

<p>That last one is a serious nod to their foresight and a judo chop to other providers. Layered Tech overs a KVM solution as a $30 or $35/day rental. Voxel automatically includes it.</p>

<p>Anyhow, with this beefy box, we set about setting up Xen. Voxel's support was admirable when my initial xen install broke the serial console access. It turned out my /etc/grub.conf needed a change:</p>

<blockquote class='code'>
title CentOS (2.6.18-53.1.21.el5xen)
        root (hd0,0)
        kernel /xen.gz-2.6.18-53.1.21.el5 <b>com1=9600,8n1</b>
        module /vmlinuz-2.6.18-53.1.21.el5xen ro root=LABEL=/ console=tty0 console=ttyS0,9600 panic=100
        module /initrd-2.6.18-53.1.21.el5xen.img
</blockquote>

<p>(Change in bold)</p>

<p>Without the com1= line, it defaulted to 38400.</p>

<p>Anyhow, with that done, I got to work actually setting up Xen virtual server instances. The host box (aka the Dom0) is CentOS, and so the "easy" install should be another instance of virtualized CentOS, although any variant should be usable. I tried to use virt-install, but it failed miserably.</p>

<p>First, I had issues with networking. Even when I resolved those, halfway through anaconda installing packages, the system crashed.</p>

<p>Ultimately, I settled on doing it manually using this guide to <a href='http://wiki.centos.org/HowTos/Xen/InstallingCentOSDomU'>installing CentOS DomUs on a CentOS Dom0</a>.</p>

<p>Caveats for me included the fact that my bridge was <b>virbr0</b> instead of <b>xenbr0</b>. I <b>have</b> a <b>xenbr0</b>, it just wasn't the correct bridge for RFC1918 going out...</p>

<p>Anyhow, aside from the networking problems, that went fairly smoothly. (Although the top of the guide has you selecting from i386 or x86_64, and the kickstart config just says i386, so I had to restart to correct that, since I was doing the x86_64 install.</p>

<p>Still on the agenda: getting a real IP address. Voxel offered a reasonable extra 8 IPs for $4/mo, and we'd like to ensure each VPS has its own IP address, not 192.168 addresses that translate.</p>

<p>Still, this has been a pretty fun thing so far. I've heard that Xen is what Amazon is using to provide AWS, and I've been learning a lot of new linux tools (like brctl; I'd never had a reason to do linux bridging prior to now, even though I've done it plenty with networking devices).</p>]]>
        
    </content>
</entry>

<entry>
    <title>jQuery 1.2.6</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/06/jquery-126.html" />
    <id>tag:www.ender.com,2008://2.102</id>

    <published>2008-06-10T05:17:54Z</published>
    <updated>2008-06-10T05:28:19Z</updated>

    <summary>A couple weeks ago, the jQuery team released jquery 1.2.6. jQuery is the write-once, run-anywhere a new type of javascript library. It's incredibly full of win. Favorite things in 1.2.6: Still small (minified+gzipped, 15k) Dimensions plugin - I can see...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
        <category term="AJAX" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Javascript" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="jQuery" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>A couple weeks ago, the jQuery team released jquery 1.2.6.</p>

<p><a href='http://jquery.com'>jQuery</a> is <s>the write-once, run-anywhere</s> a new type of javascript library.</p>

<p>It's incredibly full of win. Favorite things in 1.2.6:</p>

<ul>
<li><b>Still small</b> (minified+gzipped, 15k)</li>
<li><b>Dimensions plugin</b> - I can see why. Dimensions was a godsend, providing totally important calls like $.position() and $.innerHeight() and whatnot. Indispensible.</li>
<li><b>makeArray</b> - <i>jQuery's internal .makeArray() method now converts any array-like object into a new array. Additionally it wraps all other objects as an array and returns the resulting set.</i>  This one is so wild, it will require further digestion, but there are plenty of possibilities</li>
</ul>

<p>I'm working on a brief introductions to jQuery I will post to go over a bunch of basic techniques; things that go a tiny bit beyond the jquery.com documentation usage.</p>]]>
        
    </content>
</entry>

<entry>
    <title>jquery, the jquery form plugin, and detecting an ajax submit</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/04/jquery-the-jquery-form-plugin.html" />
    <id>tag:www.ender.com,2008://2.91</id>

    <published>2008-04-09T02:44:46Z</published>
    <updated>2008-04-09T03:14:29Z</updated>

    <summary><![CDATA[When you send a request via $.ajax, $.get, or $.post in jquery, you can test for the HTTP_X_REQUESTED_WITH header, looking for a value of 'XMLHttpRequest', a la: function isajax() { return ((!empty($_SERVER['HTTP_X_REQUESTED_WITH'])) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); } But what happens...]]></summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
        <category term="AJAX" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Javascript" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="LAMP" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="jQuery" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>When you send a request via $.ajax, $.get, or $.post in jquery, you can test for the HTTP_X_REQUESTED_WITH header, looking for a value of 'XMLHttpRequest', a la:</p>

<blockquote class='code'>
function isajax() {
  return ((!empty($_SERVER['HTTP_X_REQUESTED_WITH'])) && 
    $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
}

</blockquote>

<p>But what happens when you're using the <a href='http://www.malsup.com/jquery/form/'>jQuery Form plugin</a>? Normally, things work fine. Below the cut for a serious caveat!</p>]]>
        <![CDATA[<p>The jQuery form plugin supports posting uploads. However, when an upload is posted, the form plugin instantiates an iframe and submits that form, since xmlhttprequest can't be encoded for file uploads.</p>

<p>Therein lies the problem: that xmlhttprequest header will not be set.</p>

<p>I handled it with a hidden field:</p>

<blockquote class='code'>
&lt;form method='POST' action='/something.php'&gt;
  &lt;input type='hidden' name='isajaxrequest'
    id='isajaxrequest' value='0' /&gt;
  [...]
&lt;/form&gt;

</blockquote>

<p>Now you have a magic variable to check. Then, in your jquery:</p>

<blockquote class='code'>
$("#myform").ajaxForm() {
 [...]
 beforeSubmit: function (formData, jqForm, options) {
    $("#isajaxrequest").attr("value", 1);
})
 [...]
}

</blockquote>

<p>This way, if your form is submitted via iframe, $_REQUEST['isajaxrequest'] (in php parlance) will be non-zero.</p>

<p>WARNING: the isajaxrequest field will only work as presented here in the iframe; so you still have to test for the server header AND the isajaxrequest variable. Why? Because by the time the form plugin calls beforeSubmit, it has already copied the form into an object which will be used to serialize and submit it, and changes to the form do not propagate. What's interesting is, if you apply ajaxForm to a form with an input type='file', it will only use the iframe if that field is selected. So on the same form, clicking submit with the file selected will use an iframe, but clicking submit when the file field is empty will let it submit over xmlhttprequest.</p>

<p>You *can* still use beforeSubmit to set the field regardless, but you'll have to navigate the formData array, which is an array of js objects, like: <b> [ { "name": "isajaxrequest", "value": "1"}, {"name": "someotherfield", "value": "someothervalue" } ]</b></p>

<p>Not particularly something you want to iterate through if you can avoid it, although if you're concerned about a proxy stripping the xmlhttprequest header, you may want to do it anyhow.</p>]]>
    </content>
</entry>

<entry>
    <title>Event bubbling in Javascript</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/04/event-bubbling-in-javascript.html" />
    <id>tag:www.ender.com,2008://2.88</id>

    <published>2008-04-04T01:55:54Z</published>
    <updated>2008-04-04T02:00:11Z</updated>

    <summary>By carefully using generic event handlers, you can handle events even with a dynamic document.</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
        <category term="AJAX" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Javascript" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="LAMP" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Web 2.0" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="jQuery" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>Today I had the opportunity to play with event bubbling and applying generic event handlers. Quick overview on handling them (with jquery!) and which events cannot be cancelled.</p>]]>
        <![CDATA[<h3>Why use generic handlers?</h3>

<p>If you are making a dynamic document, you know that CSS settings are applied as entries are inserted. If you dynamically create a div with class="foo", and you have a ".foo" style defined in your stylesheet(s), then it will inherit it automatically.</p>

<p>If you bind to every class="foo" element on a page at some point (say, with $(".foo").bind() with <a href='http://www.jquery.com'>jquery</a>), then append more elements with that class, they do NOT automatically inherit the event binding.</p>

<h3>How to get around it</h3>

<p>To get around this shortcoming, you can bind your event handler to a higher level event. I'm going to describe it with jquery. For example, let's assume you want to check for clicks on all divs with a class "foo", and make the background color red.</p>

<p>Here's the 'old' way with jquery:</p>

<blockquote class='code'>
$(".foo").bind("click", function(e) {
  $(this).css({'backgroundColor': 'red'});
});
</blockquote>

<p>The problem is, if you add a new div into the DOM, it will not have that event binding.</p>

<p>Here's the 'new' way:</p>

<blockquote class='code'>
$(document.body).bind("click", function(e) {
  var $target = $(e.target);
  if ($target.is(".foo")) {
    $target.css({'backgroundColor': 'red'});
  }
});
</blockquote>

<p>Pretty simple, right? Actually, it is. And it's less code and less of a pain than all the code required to bind new elements coming in most likely. Here's the code at work. Elements with class foo are bound to change bgcolor to red when clicked:</p>

<blockquote>
<iframe width='500' src='http://www.ender.com/~matt/bubbleDemo.html'></iframe>
</blockquote>

<h3>What can go wrong?</h3>

<p>The major problem is events that don't bubble up. The <a href='http://en.wikipedia.org/wiki/DOM_Events'>wikipedia entry on DOM events</a> has a nice list of events with their Bubble property - among those that <b>don't</b> bubble up: load, unload, focus (which was the one that prompted me to investigate), blur, DOMNode(RemovedFrom|InsertedInto)Document, rowexit, and others.</p>

<p>Still, this is an awesome technique. The more you make your page dynamic, the more useful it becomes.</p>]]>
    </content>
</entry>

<entry>
    <title>Put this in your toolbox</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/03/put-this-in-your-toolbox.html" />
    <id>tag:www.ender.com,2008://2.87</id>

    <published>2008-03-20T16:11:14Z</published>
    <updated>2008-03-20T16:13:01Z</updated>

    <summary>I'm going to admit to not having used this yet. But having used a lot other solutions (most recently pear Safe_HTML with additional pre/post-processing), this is a breath of fresh air: HTML Purifier...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
        <category term="LAMP" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="PHP" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>I'm going to admit to not having used this yet. But having used a lot other solutions (most recently pear Safe_HTML with additional pre/post-processing), this is a breath of fresh air: <a href="http://htmlpurifier.org/">HTML Purifier</a></p>]]>
        
    </content>
</entry>

<entry>
    <title>Hello again, Hello World</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/03/hello-again-hello-world.html" />
    <id>tag:www.ender.com,2008://2.83</id>

    <published>2008-03-11T03:05:42Z</published>
    <updated>2008-03-11T03:09:47Z</updated>

    <summary> Any programmer has done this one a thousand times. But this one is more fun than I remember this being before....</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img alt="hw.jpg" src="http://www.ender.com/photos/hw.jpg" width="644" height="776" class="mt-image-right" style="float: right; margin: 0 0 20px 20px;" /></span></p>

<p>Any programmer has done this one a thousand times. But this one is more fun than I remember this being before.</p>]]>
        
    </content>
</entry>

<entry>
    <title>MT 4.1</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2008/03/mt-41.html" />
    <id>tag:www.ender.com,2008://2.81</id>

    <published>2008-03-09T03:50:23Z</published>
    <updated>2008-03-09T03:52:05Z</updated>

    <summary>So, I've upgraded to Moveable Type 4.1, and I'm forcing commenters to use on the of the web login services (typekey, oid, etc). If that doesn't cut down on the spammers, then it's time to drop the hammer and put...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>So, I've upgraded to Moveable Type 4.1, and I'm forcing commenters to use on the of the web login services (typekey, oid, etc). If that doesn't cut down on the spammers, then it's time to drop the hammer and put in an anti-spam plugin. But at least now that I am upgraded, getting such a plugin will be easier.</p>]]>
        
    </content>
</entry>

<entry>
    <title>MySQL Update Evaluation Gotcha</title>
    <link rel="alternate" type="text/html" href="http://www.ender.com/2007/08/mysql-update-evaluation-gotcha.html" />
    <id>tag:www.ender.com,2007://2.66</id>

    <published>2007-08-13T23:45:21Z</published>
    <updated>2007-08-13T23:48:53Z</updated>

    <summary>If you use MySQL, note this: mysql&gt; create database test; Query OK, 1 row affected (0.01 sec) mysql&gt; use test; Database changed mysql&gt; create table foo ( -&gt; x int, -&gt; y int -&gt; ); Query OK, 0 rows affected...</summary>
    <author>
        <name>Matt</name>
        <uri>http://www.mattwallace.net/</uri>
    </author>
    
        <category term="LAMP" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="MySQL" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://www.ender.com/">
        <![CDATA[<p>If you use MySQL, note this:</p>

<div style='background-color: #202020'><blockquote><pre>

<p>mysql> create database test;<br />
Query OK, 1 row affected (0.01 sec)</p>

<p>mysql> use test;<br />
Database changed<br />
mysql> create table foo (<br />
    -> x int,<br />
    -> y int<br />
    -> );<br />
Query OK, 0 rows affected (0.00 sec)</p>

<p>mysql> insert into foo values (1, 2);<br />
Query OK, 1 row affected (0.00 sec)</p>

<p>mysql> select * from foo;<br />
+------+------+<br />
| x    | y    |<br />
+------+------+<br />
|    1 |    2 |<br />
+------+------+<br />
1 row in set (0.00 sec)</p>

<p>mysql> update foo set x=5,y=10,x=x+y;<br />
Query OK, 1 row affected (0.00 sec)<br />
Rows matched: 1  Changed: 1  <b>Warnings: 0</b></p>

<p>mysql> select * from foo;<br />
+------+------+<br />
| x    | y    |<br />
+------+------+<br />
|   15 |   10 |<br />
+------+------+<br />
1 row in set (0.00 sec)</p>

<p>mysql><br />
</pre></blockquote></div></p>

<p>I bolded the part that bothers me. No warnings. Double-updating the same value in the same row in the same statement throws no warnings (never mind errors!).  I haven't checked the ANSI standard to see if this is mentioned, but it sure is worth noting.</p>]]>
        
    </content>
</entry>

</feed>
