Most read posts this month



Apr 30th 2009 × javascript obscure exception trapping and reporting via AJAX for mootools

I’ve had this post in my submission bin for a while but a question of the digital point forums today prompted me to dig it out… First of all, what is the difference between a javascript exception and a javascript error? Well, my view on this is simplistic: an exception IS an error of a runtime nature that can be handled, whereas an error is typically to do with syntax and can’t be ‘caught’.

So why would we care of exception errors anyway?
Whilst programmers take every possible measure to release compliant and bug-free code for all possible browsers, the sad truth is that–for ajax-rich applications–no amount of testing can ensure things won’t break for somebody. There always will be the ‘daft’ people, their quirky old browsers (IE6, anyone?), odd plug-ins, proxies / anti-virus software, leap years and so forth–somehow managing to trigger unexpected javascript exceptions. The more traffic you get, the better the chances are for somebody to stumble upon such difficulties. What you don’t always get is people phoning up to tell you about it. Not unless it’s something critical – like the inability to add an item to a shopping basket or failure to checkout. And let me tell you, your customer services won’t be able to get any meaningful debug information.

A practical example?
Recently I got a report of a customer that was unable to checkout and for whom the ‘checkout’ button did nothing. The button itself had an onclick event assigned to it within the mooTools domready function… Surprisingly, it turned out the domready just consistently fired up earlier than expected for him, with the checkout.gif image just not being a part of the DOM at the time of the event assignment. This got me worried and made me want to try and discover other such potential deterrents to people’s browsing experience.

The solution is closer when you know your problem areas…
I created a javascript exception trapping handler and placed it around what was perceived to be the ‘dangerous / unsafe’ portions of the checkout code. The handler itself was set to use XHR (ajax) to send me the exception data via email, alongside of whatever additional data the PHP script was able to grab (browser, user info, basket contents and so forth). The results were… astonishing. This quirk was happening a fair bit more than we would have liked (1 in 30 customers). We also found out that our promo codes buttons were also being affected… The flaw in the mooTools 1.11 domready code aside, how did we fix it? By not assigning events until all the elements were available, by using load as opposed to domready for IE users and so forth.

Here is the refactored code for mootools 1.2+ instead:

var errorHandler = function(dbug) {
    // trap errors and send via ajax to a script that logs them
    new Request({
        url: "/errorTrap.php",
        data: dbug,
        onComplete: function() {
             $("errorOutput").set("html", this.response.text);
        } // comment this function out out and the comma on the line above. data will arrive via $_POST.
    }).send();
    return true;
}; // end errorHandler

$("makeException").addEvent("click", function(e) {
    new Event(e).stop();
    try {
        duffColours[2] = 0;
    } catch(er) {
        errorHandler(er); // install it.
    }
});

And here is an example of the errorTrap.php contents, I’d advise you to do data washing/escaping as well:

<?PHP
$body = "ERROR DETAILS FOLLOW:\n\n";

foreach($_REQUEST as $key => $value) {
    $body .= "$key: " . str_replace('"', '\"', $value) . "\n";
}

$body .= "\n----------------------------------------------------\nServer variables:\n\n";
foreach($_SERVER as $key => $value) {
    $body .= "$key: " . mysql_escape_string($value) . "\n";
}

mail("your@email.com", "JS exception on {$_SERVER['HTTP_REFERER']}", $body);
?>

To create an exception, click here – it will send it to a file called errorTrap.php, which iterates the $_GET and $_SERVER variables before dispatching me the email

Error output here


Apr 27th 2009 × microsoft internet explorer 8 finally released and some IE6 LI floating quirks

Some web dev I am–thanks to google adwords I on this very site I only just noticed an advert by microsoft for downloading IE8… Not how I should have found out about it but it beats not knowing at all, I guess.

I wonder how many issues this will introduce and how complicated my life will soon become… grab it if you have to (no follow link via google)

*update* as I went over to my Microsoft Virtual PC in order to test IE8, I accidentally loaded this site in IE6 and IT WAS HORRIBLE. In particular, it failed to inherit the width of the main content div so it spilled and defaced the site each time any code got posted (affected were code, blockquote and pre tags). Sigh… apologies, reparations have been done now, I just tend to ignore IE6 even though it still amounts to almost 15% of visitors… Even though it has been fixed in part, I should perhaps think of a gentler degradation for IE6 users, one that offers them a chance to upgrade what is now an 8 years old system…

Speaking of IE6 quirks, I had to do a semantic tab system for mootools 1.11 the other day and discovered how IE6 completely messes up the display of LI elements in an ordered list when having nested elements, spilling to the whole available space on the current line instead. The only fix for this is to float the children elements as well… For example:

<li style=”float: left”><strong>this is some text</strong></li> won’t just assume the text’s width, it will span over the whole available width and will push down a line the next LI. You need to float the strong as well in order to achieve the desired effect… Pants!


Apr 24th 2009 × mootools 1.2.2 released

Just a quick update, in case you missed it: mootools 1.2.2 is out, mostly a bug fixing release. Also, the new mootools-more is out. So… check them out.


Apr 15th 2009 × notiMoo: mootools notification in the style of growl

Just caught wind of this one on ajaxian, notiMoo 1.1 is a mootools 1.2 class that easily allows you to display notifications and messages to your visitors. The good thing about it is that they can be as intrusive and difficult to ignore as you need them to be, or vice versa. Worth knowing about anyway…

If you don’t know about grow, it’s an apple thing. Again… Don’t you just hate it that so many of the really good interfaces out there are born somewhere within the Apple empire…


Apr 7th 2009 × mootools image cropping: kroppr, mooCrop and UvumiTools Crop

This week I am doing some CMS changes that are likely to warrant image cropping on the fly, so I had a look around for any mootools classes / plugins that can help with this.

First, I stumbled upon a nice mootools based image cropper, or shall I say ‘kroppr instead. What’s nice / special about it is that in addition to being able to crop images, it can rotate, zoom and so forth – and WITHOUT flash or java applets.

I’d pimp it more but it’s also not free, there a cost of &eurol29.00 per subdomain for this to run. Still, if you are ever after such a tool, it’s worth knowing about it.

*EDIT* removed link from this script as its reported as a rip-off.
*update* makelele sends word of his own ‘mootools cropper’ _link removed_ which is just as capable, a solution mix of PHP (imageMagick php ext) and javascript, with the licensing cost of €25.00. It’s a market economy so the choice is yours.

If you don’t fancy spending, you can always use Nathan White’s FREE and excellent mooCrop (you can also follow NW on twitter, he tweets a lot :D )

And another free one that seems to have similar features, UvumiTools Crop. Just click and pick…