Follow me: @D_mitar

Most read posts recently



Sep 29th 2008 × What is a javascript framework and why do I need one, part I

This ‘article’ is aimed at helping webmasters, programmers and designers who already have a rudimentary understanding of javascript and DOM programming in cementing their knowledge and getting a solid base for building browser apps in the future. For illustration purposes, I shall be using the MooTools framework but this does not mean it’s the only sensible choice.

I have been posting a fair bit on DigitalPoint’s javascript forums recently and was amazed to discover (well, not really) the huge amounts of people that struggle to get basic javascript bits and bobs working. Adding a javascript framework won’t necessarily fix that all of these problems but a good half of them (at least) will get ‘nearly there’. It also proven to improve manhood tenfold…

So, what is a javascript framework anyway?
In a nutshell: a library of pre-written javascript controls, functions and methods that make it easier for the developer to quickly and accurately produce cross browser compliant code. Example popular frameworks are: YUI, ExtJS, Prototype, MooTools and of course, jQuery. There are also several smaller frameworks around – have a look on ajaxian for a recent rundown of the relative unknowns (like Capuccino)

Yeah, and? What do I need all these libraries for?

Selectors

making sure you can access any and all of your DOM elements at your fingertips

Typically, frameworks provide all sorts of shortcuts for accessing elements within the DOM. These are called selectors and are similar to CSS selectors. Example:

<div id="mydiv" class="myclass" rel="89">click me</div>
<div id="mydiv2" class="myclass" rel="90"><a href="#">null link #0</a></div>
<span id="myspan" class="myclass" rel="89">hello world 3</span>

<a href="#">null link #1</a>
<a href="#">null link #2</a>
<a href="/index.php">normal link</a>
<input id="name" type="text" class="textfield" />
<input id="surname" type="text" class="textfield required" />*
<textarea id="message" class="required"></textarea>*

… and mootools selectors on the above

var div = $("mydiv"); // just reference by id, same as document.getElementById("mydiv"); 

// get an array of all elements that are 'myclass' (mydiv, mydiv2, myspan)
var elsArray = document.getElements(".myclass"); 

// get an array of div elements that are 'myclass' (mydiv, mydiv2)
var elsArray = document.getElements("div.myclass"); 

// get an array of elements that are myclass and rel == 89 (mydiv, myspan)
var elsArray = document.getElements(".myclass[rel=89]");

// get an array of elements that are myclass and rel is not 89 (mydiv2)
var elsArray = document.getElements(".myclass[rel!=89]"); 

// get the first element that has class myclass and rel=89 (mydiv)
var elObject = document.getElement("myclass[rel=89]");

// get an array of null links (to anchors) - (null links #0,1,2)
var links = document.getElements("a[href=#]");

// all links within the mydiv2 layer only
var localLinksArray = $("mydiv2").getElements("a"); 

// get all required fields (surname, message)
var requiredFields = document.getElements("input.required textarea.required");

The examples for selectors are potentially endless as are the possiblities for use. There is nothing to prevent you from using the old document.getElementById() function, or even document.getElementsByTagName(). There is even a new function coming out now to a browser near you (perhaps) – getElementsByClassName(). But that’s just the point – by using a framework, you are guaranteed compatibility NOW and with future releases. If a new browser ‘Steel’ comes on the market that provides access to its DOM elements by way of a function document.ById(), you would not have to go and update every single bit of javascript that on your sites to provide support for it – all you’d do is update your framework and grab a coffee – the major frameworks will always pickup on browser changes and new versions. In fact, I expect most are readying updates to cover the new Google Chrome browser and webkit changes.

Compatibility and performance

Different browsers and different versions have always had their old ideas about what’s the best way to ‘do things’. One can’t help but apploud the peioneering spirit of the Internet Explorer engineers, who pretty much just made stuff up as they went along and ignored many a WC3 standard in the process… The thing that frameworks give you that you will learn to appreciate the most is a bridge between all the browsers. You no longer need to worry if IE6 will fire an onload event when other browsers fire oncomplete. You also won’t have to worry about mistakes in misfiring of onmouseout events when hovering over child elements (IE bug, of course). Frameworks do the worrying for you and fix those common errors that are deemed fixable.

As for performance, it really, really can’t get much better than relying on a bunch of crazed javascript coders and programmers, many of whom are involved in the development of Mozilla, to write, refactor and improve all your functions for you! You get what you want from the DOM, in the fastest possible way. Yes, I am talking ‘selectors’ again and there are lots of test suites / javascript framework benchmarking results of all the major frameworks. More or that will come in part 2 – for now, suffice to say: doing a for() loop through your elements just won’t cut it.

AJAX out of the box

Enough selecting, it’s time to use some keywords that spell ‘magic’ to any marketing pro out there: ajax. web 2.0. ooooh….

Most of today’s modern frameworks will have a way of dealing with XHR requests, more commonly referred to as AJAX. Whereas it does not take much to get going with a simple AJAX request, frameworks have it polished into perfection. You can easily set things like:

  • method (GET / POST)
  • target url
  • setting up events (start, sucess, failure)
  • parameters to url as GET or POST data
  • displaying desponse into a layer/table element
  • evaluating response as javascript in a safe way
  • processing response AND evaluating <script> tags

    Say, you have an element with some summary text and you’d like to attach a click event to it to show the full article text. Here is how that AJAX request can look in mootools:

    $("mydiv").addEvent("click", function() {
        // make a click on mydiv2 to callup an ajax function that gets
        // the full contents of the layer replaced and runs through some scripts
        new Request({
            url: "/index.php",
            method: "get",
            evalResponse: false,
            evalScripts: true,
            update: $("mydiv"), // replaces the text there at the moment
            onComplete: function() {
                // already updated to full text, no need for the event to remain
                $("mydiv").removeEvents();
            }
        }).send("action=getFullText&id="+this.get("rel"));
    });

    Same functionality (just the AJAX call) in jQuery would go like so:

    $.ajax({
      url: "index.php",
      cache: false,
      success: function(response){
        $("#mydiv").append(response);
      }
    });

    In part 2, we will review how to code a few practical snippets that can build you a core library for your site – things like, tooltips, bubbles, form checkers, expanding and collapsing menus and more. And of course, my advice on choosing the right framework for you (always a subject of debate and controversy)


Sep 22nd 2008 × chelsea vs united: we got RILEyd

Manchester United are lucky to walk away with anything from the bridge, thanks in no small part to the antics of Mike Riley… It would be the understatement of the year to say I am happy with the result, Chelsea played better for the last hour of the game and deserved at least a couple more goals… Dubious penalty claim aside, how can United just get the 7 yellow cards (yes, all in a single game–they WILL get a £25,000 fine for it)? Even in basketball has greater “player control” with its liberal 5 “fouls & out” system. And the final whistle blown before the free kick awarded can be taken really confused everyone on the pitch… But the important thing is to follow Fergie’s instructions and protect Ronaldo–he should get the benefit of the doubt and the decision because he is so special–even when it’s clear to everyone he is diving…

Anyway, we all know which Manchester team will be the one to contend the title with soon…


Sep 17th 2008 × largerBox: a mootools apple-style modal lightbox effect

**updated 2-nd May 2011**
This is getting completely overhauled. Basically, I am currently working on a new mootools modalBox Class called ‘baseBox’. You can see it on jsfiddle and play with it some. Full details, documentation and more examples will come when I release it as an official plugin on the MooTools Forge.

largerBox is now deprecated. Read this for more info.

mootools modal box class

I saw the animation apple do when viewing a trailer (non-HD) on their modal box and decided to replicate the effect for my image viewing in mootools.

Examples of largerBox v3.0 (animations / morphs / tweens don’t work too nice over iframes, mind)


UPDATE – 24/11/2009 largerBox is in the process of being refactored. largerBox v3 – beta 1 is available to look at and play with. It features a totally drag and drop solution – CSS is built into the javascript class, so are the images for next/previous and close. That’s right, there are no files to edit, updated, upload — aside from your images. Check the new source here: /js/largerBox.js

How does it work?
In the normal way, it takes a <a href=’imagename.jpg’> … </a> and attaches itself to the click event, not interfering in any way with your html. All you need to do is add ‘class=”fullimage”‘ to the anchor tag and fraggedBox will do the rest.

What do I need to get this to work?
You need to grab a copy of mootools 1.2.x – core.

You then need to grab the latest beta. Just look at the demo page anyway…


Sep 16th 2008 × quakelive. er, quake, in a browser. whatever next?

QuakeLive. It’s… odd! I am not sure I approve… Here is a video of the quakecon 2008 quakelive ctf finals, make your own mind up. Personally, I still think Q3A is the greatest game of all time… But this looks mighty close!

no

Sep 16th 2008 × My apple powerbook died. I hated it anyway

In a way, I am happy that it died, now I can leave it in front of our local charity shop and make a homeless person very happy. Or… just one of those dodgy twats that keep on stealing from there at night.

This image was created to commemorate the occasion and the launch of my new “Apple for homeless” campaign:


p.s. It was not really mine, I’d never buy Apple – had to use it for Safari testing at work.


Sep 16th 2008 × Hate the new facebook design

It’s just silly and totally unnecessary unless you have some silly amount of applications… For the rest of us who use facebook to keep in touch and play a bit of poker, it just sucks. The sad truth is: they are freeing up space to have more adverts. Not that it matters with firefox anyway!


Sep 15th 2008 × Google analytics script via domready and mootools

Hate it when your site display is being slow while it waits for google analytics?

Google Analytics is just great. But, regretfully–it often causes a delay in my sites’ display. Loading the remote script can pause the domready (as the browser waits to see if the script it is evaluating won’t affect the DOM in some way). The result: often page load times increase by a few seconds.

But there is a simple fix. Here is sample the code provided by google to include into your page:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-1199722-1");
pageTracker._trackPageview();
</script>

The solution for a nice fast page load: go to domready!

Simply rewrite this as a part of your normal domready function:

// google analytics via mootools / domready.
window.addEvent("domready", function() {

    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", {
        onload: function() {
            var pageTracker = _gat._getTracker("UA-1199722-1"); // your id here
            pageTracker._initData();
            pageTracker._trackPageview();
        }
    });
}); // end domready

Google won’t ever interfere with your page load again!

no

Sep 15th 2008 × thawte site seal via domready

unfortunately, we have no Thawte certificate ourselves so this demo will show the badge as ‘invalid certificate’.


The problem with the Thawte Site Seals…

After having developed a site for a client, we were asked to include the THAWTE site seals that verify the site’s authenticity. That is all fine, but the seal uses

<script src="https://siteseal.thawte.com/cgi/server/thawte_seal_generator.exe"></script>

tags and holds the DOM load, often causing a performance drop — especially so if Thawte’s server is running slow and your page waits on the DOMREADY event….

The solution for a nice clean seal: Get rid of the sealgenerator.exe

Remove the code supplied by thawte code and replace with this goodness:

window.addEvent("domready", function() {
    // written for mootools 1.2
    var doSeal = function(targetObject) {
        // show thawte seal code w/o the reference to their exe in a dom compliant way
        var loc     = window.location.href;
        var sealLoc = "https://www.thawte.com/core/process?process=public-site-seal-cert-details&public-site-seal-cert-details.referer=" + loc;
        new Asset.image("https://siteseal.thawte.com/site-seal/image?g5ff7fe3df88d8fafaa6035dc97152679ga45f4b102502fefbd20332591412323c" + (new Date()).getTimezoneOffset() + "g5d43a4e836d7d07a50759fb1da9cc900g1a0452b749ddb29083736b34ef4a14e9ga45f4b102502fefbd20332591412323c" + loc, {id: 'sealimage'})
            .inject(targetObject).addClass("cur").set("title", "click to verify").addEvent("click", function() {
                window.open(sealLoc,'ga3ef4eb41dcac740b918440eb9cd19c8','height=500,width=516,scrollbars=yes,status=1');
            });
    } // end doSeal

    // embed it...
    doSeal($("seal"));
});// domready


Sep 15th 2008 × Ecommerce platforms: BossCart JV

As I am pushing on in my quest to find a perfect free e-commerce platform that has great SEO, ease of product, brand, stock and category management with industrial strength order management. Not an easy task, to be sure… The latest package I’d like to talk about today is BossCart JV by BossCart

What I had presumed to be a lightweight version of their bespoke commercial product is actually quite different. For starters, it uses mootools (1.11) and the “commercial” cart is under jQuery but let’s not draw any conclusions on the strength of the frameworks based upon this just yet, heh :) . It also seems to lack one very important facet of trading: brands/manufacturers (this has been left for the ‘full’ version once again). Other than that, first impressions as a user: it appears to have been coded within the spirit of Web 2.0 in mind – search tags, lightbox imaging, product ratings, the chunky yet slick looks…

Since this IS fragged.org, we write about what we don’t like first and assume the rest is fine or dismiss it as boring… With that in mind, let’s pop the bonnet and see if this baby can organically give good SERPs. n.b. you can always fix the css/theme so this is very superficial

I picked http://jv-cart.bosscart.com/golf-iron-supplier as the page to disect first.

I always wondered what JV stood for and some light got shed here:

<img src="http://jv-cart.bosscart.com/components/com_virtuemart/shop_image/product/0a2d9237bb75f96c09db6f5d91de7287.jpg"  width="135" border="0" alt="Titleist Pro V1 Premium Refinished Golf Balls" style="float:left"/>

Seems that at least in part, the jv-bosscart are based on Joomla / Virtuemart. This explains a lot – mootools 1.11 and not 1.2, for starters (Joomla have yet to move over).

The URL is SEO friendly enough, although the title does not appear to be even remotely relevant. We find this:

<meta name="description" content="" />
<meta name="keywords" content="sample Boss Cart JV golf shop,http://jv-cart.bosscart.com" />

It may seem like a little thing to simply go and set it manually – but the last e-commerce site i worked on had 1200 products and 50+ categories. Today’s e-commerce software needs to try and back the site owners up and fill in wherever possible…

The default theme seems to have a topbar and a side menu element floated to the left, with the body of the page that follows. Obvious disadvantage of that is that repetitive text (categories, site pages, header bits) will always precede the real important page headings and body texts that will determine the page relevance.

The page also appears to be using a mix of css-driven-design and tables, as well as plenty of inline css.

Also stuff that you can clean up as you customise it. Links not having title tags is yet another hindrance that will need fixing.

The first real page-relevant bit of code in the source is at line 228 (a bit too far down for my tastes). The most important dynamic content, keywords, heading tags, descriptions – they should be as close to the top of the page as possible and without too much markup. This may be a semantic view but it works.

The questions here is, are there good enough framework templates to get you started re-skinning the default one?

How effective is the organic SEO out of the box? I decided to search on google for the products in the test shop – obviously w/o any proactive marketing as examples: Bay Hill Plasma irons comes as no. 1, Bay Hill Irons as the 4-th site down. This may be due to the number of inbound links to the BossCart site but it’s also no fluke. The product descriptions are explicit and help matters also. Titleist Golf Balls comes up also on page 2.

The provisonal verdict for BossCart JV: There are a lot of areas that need work – messages here and there, visual glitches, optimisations and other bits. We’ve not looked at the admin interface yet — but it has potential. I would say, as an out-of-the-box free solution, it probably does more for a startup business than oS Commerce or CubeCart.

I will post updates here as soon as I find more about it.


Sep 15th 2008 × Mootools 1.2 is slowly finishing off the community

There is simply nothing remotely as good as the mootools javascript framework out there. And version 1.2 did nothing but make it even more fun to code for. That aside, the mootools team decided to ditch the forums and move over to google groups.

I have been subscribed to the new mailing list since the start and I can see it slowly subsiding in interest and usefulness already: what precious little sense of community building i felt before is now slowly diminishing… A terrible way to alienate a loyal userbase who LOVED the old forums – People (well, I did anyway) used to browse them for the sheer fun, to get or offer help, to showcase work, to find out more about new mootools sites or plugins…

There is now a half-hearted attempt at making an unofficial forum but it is just another wrong decision that takes away even more people from the mootools google group…

Shame, really.

*update* some bloke I had not heard of (does not mean much really, I live in a box) called Tom Occhino of the mootools team posted this thread on the mootools “forum”/mail list. I am sorry to say, Tom: claiming that frameworks like jQuery don’t have forums and their users don’t bitch about it are unsubstantiated. For starters, they never had a forum to begin with so they wouldn’t feel as if something precious has been taken away from them. I believe a number of people will have chosen mootools due to the community that it had. Not the best commercial / career decision, all you have to do is look at any job site / agency: jQuery is sought after far more often than mootools (wrong as that might be). Even recruiters have heard of jQuery despite not being able to tell you what a framework is… So why disband the community and try to be another jQuery? This seat is already taken…