FireFox 6 Beta??

by Developer @ pebbl.co.uk on Saturday, 9 July 2011

Heh, ok so I'm sure I'm not the only one to have been surprised-of-late by FireFox's sudden interest in incrementation. They've gone a bit +1 all in the name of Rapid Release..? It seems more to me that the browser developers are just feeling inadequate about the size of their version numbers - the whole "Mega Pixel" irrational again.

Quite why Opera's double figures never made the Mozilla foundation flinch before i don't know... But I guess Chrome must really be worrying certain people. Not sure why myself, any browser that keeps overriding my typed location with an erroneous "empty tab" does not make it to the coveted "Default Browser" status as far as I'm concerned.

Skip to the end...

Anyway, all the above nonsense aside, something interesting did pop up from all these "upgrade to the latest beta" popups. It seems the latest FF v6 has added support for something I had totally managed to miss before now...

window.matchMedia()

In short this should help a great deal when dealing with projects destined for use on a multitude of devices. Put simply, It allows you to harness the power of media queries from within JavaScript (so no longer just CSS) and not only that, it also allows EventListeners to be added to these media queries - so state change can be detected easily. Modern day web developers really don't know how easy they have it ;)

For more information about this new addition check the link below:

http://dbaron.org/log/20110422-matchMedia

PS:

As a side note to this, and just in case you don't know - there are a few other functions in a similar vein (i.e. CSS abilities made available in JavaScript) which have sprung up (possibly due to jQuery/Sizzle's prevalence.. I'm not sure) but one I keep meaning to sit down with and see how well it works is the following:

Window.querySelector()

Basically exposing something that (looking back) should always have been accessible since the advent of CSS - just like Document.getElementsByClassName() - which is the ability to use the power of the CSS selector engine from JavaScript.

https://developer.mozilla.org/En/DOM/Document.querySelector

Internet Explorer stylesheet inject glitch

by Developer @ pebbl.co.uk on Monday, 27 June 2011

:: This doesn't work in IE... 8???

One more random glitch to appear out of nowhere, I guess it just goes to show how much more front-end work I've been doing recently :) Even though today's browsers are much more converged than their forerunners, there is still a veritable selection of quirks that need to be kept in mind.

For example, If you are having problems with IE7/IE8/IE9 when trying to inject a stylesheet link tag in to the DOM (either in the head or body of a HTML page), then look no further than this post - unless of course this doesn't solve your problem, in which case you are free to carry on googling.

:: The Problem

I was going in circles last night working on my new site, trying to get my JavaScript enhanced css to work in Internet Explorer 8. On top of some bespoke code to determin certain things about the current user agent, I was using jQuery to append conditionally chosen stylesheets to my DOM. Everything was fine in FF, Safari, Opera and Chrome - but IE refused to import my "core-ie.css".

Here is the offending line:

$('head').append(
   '<link rel="stylesheet" type="text/css" href="css/core-ie.css" />'
);

Now I know I've used the above construction before, and it has worked fine in IE. And as any programmer knows, there is nothing more frustrating than something that "should be working". So I went round in circles debugging, inspecting, tracing and alerting only to find what I already knew to be the truth - jQuery and my bespoke code were pretty much 99.8% cross-browser and everything I was asking for was actually happening. i.e. my link element was being injected in to the DOM. (Don't be fooled by IE8's inspector, it refused to display the added link element at any time - I resorted to alerting out the innerHTML of the head to discover the truth).

So the link tag was definitely being inserted in exactly the way I wanted... it was just being totally ignored for some reason? Now obviously at this step I thought maybe the problem resided in my CSS, maybe the style alterations I wanted were being overriden. So - as you would expect - I changed the CSS content to hide all child elements, and highlighted the body with a bright pink background and dotted orange border. No way that could be missed ;)

Much to my annoyance, this too was utterly ignored. So an exasperated then, and many googled variations on the theme of "append stylesheet jQuery IE8 problem" later; I came across one little comment (on a little page) that totally sorted my problem out... and probably could have been explained here in about 5 lines; that is if I didn't like to waffle.

:: The Solution

I do not know which versions of IE are specifically affected - I haven't had time to test and find out, but it seems that when using .innerHTML (that good old js property which jQuery makes good use of) IE will treat paths for certain elements as absolute URLs - even if they are quite obviously relative.

So that solution I mentioned before? Well there's two - and now you know what is actually causing the problem they should both be straight-forward.

  1. Don't use relative paths on link elements when injecting them via innerHTML
  2. ... or ... Don't inject link elements using innerHTML, create them using createElement and append them properly to the DOM.
function loadStylesheet( href ){
   var ss = document.createElement('link');
       ss.type = 'text/css';
       ss.rel = 'stylesheet';
       ss.href = href;
   document.getElementsByTagName('head')[0].appendChild(ss);
}

Opera 10.10 and multipart form posting problems...

by Developer @ pebbl.co.uk on Sunday, 26 June 2011

Yet again another problem I've come across recently in my travels. Implementing a YouTube Video uploader for work (via Zend/gData API) I found that the standard example form that YouTube/Google suggest for posting videos just failed to work entirely in Opera. Albeit, I was running this in a localhost environment on my MacBook (powered by MAMP) but out the of nearly 10+ different senarios I set up to test this problem, all failed (senarios that all worked on the other top browsers).

Opera did seem to action something when the submit button was pressed (the browser started loading), and I do have to admit I didn't actually test to see if the Videos where actually getting through to YouTube - but the most worrying thing about this problem is what happens next, after the browser has finished it's pretty loading animation...

. .. which is absolutely nothing! The screen just would not refresh to show the newly downloaded page. Instead the old page would remain.

To make matters even more confuddling I ran a test that involved changing the target of the form to open a new window. This did indeed open the new window, but the new page was entirely blank, and inspecting the Dom returned an error in Dragonfly stating something along the lines of "This window has no runtime".

Dragonfly also wouldn't track that a HTTP request had occured - subsequent clicks on the submit button would trigger HTTP calls that were tracked (and that return a succesful URL) but the page still would not refresh/redirect to the newly downloaded page. Very odd...

The only time I could get the form to actually properly post and redirect was when YouTube returned a failure code (400,500 ...) so this led me to believe that Opera has some problems when dealing with upload URLs that 302 redirect somewhere else - which is exacly what YouTube does.

I only had a short time to look in to this, and haven't tested on a live linux server set up yet (that should happen next week) but so far I could not find rhyme nor reason for this strange behaviour (even after extensive Googling). My only option so far has been to disable the video uploads specifically for Opera.

Unexpected behaviour when mixing setTimeout and .apply()

by Developer @ pebbl.co.uk

Recently I came across this - presumably the side effect of security measures - but it seems that unlike other functions, you can't just override the "this" object on many of the native functions (i.e. setTimeout) in most modern browsers (using either the call or apply methods of the Function object).

Now you might ask why you would want to do that, and yes... I can't really give you an example if I'm honest (save for rather mental user-agent detection) - but the thing that I find strange is that whilst you can't set "this" to any of the standard variable types, you can set it to either "null" or "undefined". If I was to read through the actual source of the browsers, I'm sure I would find the reason for this... but I just found it odd logic. In my mind any change should either throw a security exception or not, but anyways.. just thought I'd mention it.

On the following link you should see some coloured squares - if you hover (and wait for the title text to appear) you'll see a description of what was tested and any error that was tripped. The green squares mean the setTimeout fired with it's this object set - the red means it failed / caused an exception. If you test different browsers you'll see that MacBook OSX {FF4/FF5, Safari5, Chrome(12.0.742.100)} all agree whereas MacBook OSX {Opera 10.10} allows the basic var types but nothing complex, and Dell Lattitude WinXP {IE8} doesn't even support the apply() method.

Tests

Pebbl Updated

by Developer @ pebbl.co.uk on Thursday, 9 June 2011

I put the latest changes live to pebbl.co.uk today - this list covers just a few of the new abilities that my CanvasTheory library now supports (thanks to the Pebbl project). Please bear in mind that this library is completely "work in progress":

  • Redraw Regions : before I had gone the lazy route of just clearing the entire canvas prior to the next redraw. I knew at the time that I was going to have to be un-lazy later on - but I didn't quite realise how quickly I would have to make the change. Initially I was developing on my Mac Book, but when I tested the game on a widescreen monitor the slow down was drastic. So CanvasTheory now supports clearing only the regions it has drawn. The next step will be to add intelligence this so that CanvasTheory knows whether or not the Sprite has changed.
  • Pixel Perfection Collisions : This is something I knew would always be needed for a game that involved a little blob bouncing on floating rocks - hence the reason why I chose to build Pebbl using Canvas rather than using any old DHTML. So to cut along story short Pixel Collision Detection is now a supported feature - but only partially - don't go rotating the Sprites and expecting things to work just yet ;)
  • Sprites & Layers:

    I've implemented a flexible sprite based system for CanvasTheory to use - backed up by a weaker layering system (something I hope to improve upon as time permits).

    What makes CanvasTheory's Sprites flexible? The fact that they are each uniquely tied to a HTML element. This allows for jQuery animate and selector support, for future DHTML fallback (if Canvas support fails) and for handling native DOM events and event bubbling. The way CanvasTheory is coded - with it's ExtendTheory counterpart - the Sprite code is also extendable, so Specific Sprites can be created quickly and easily.

  • Pixel and Shape Masks : This is a brand new feature that is still work in progress. It was developed to support the "Black Holes" in Pebbl, allowing the main character to seemingly fall through holes in the scenery. Whilst "Pixel Mask" definitely needs optimisation work, the Shape masks are working well.
  • Hotspots and easy translation : A somewhat untested feature is the ability to shift a Sprite's positional hotspot (and whilst this seems to work, it may cause problems when mixing translations). I've also tried to make the Sprite interface as easy to use as possible, allowing for quite a few shorthand functions.

At last, something a little more flexible...

by Developer @ pebbl.co.uk on Monday, 6 June 2011

I know it might not sound like something to rally much interest, but when you spend much of your working life submerged in particular technologies - using them to find the best solution to a multitude of problems - it's always nice to hear about a new way to achieve something. Especially when that "certain something" has seemed almost too obvious for words, and really should have been implemented years ago...

I stumbled across this rather useful new strand of the W3C CSS proposals completely by chance - I can't quite recall what it was that I was looking for at the time - but it definitely wasn't anything to do with an entirely new box model. And especially not the kind of box model that could put an end to a great many HTML layout problems; problems that happily fuel the frustrations of web developers everywhere.

Now I've discovered new properties and attributes before, or even completely new methodologies - only to read further in to the documentation and find that the proposal has either been thrown out or isn't even a gleam in any self-respecting browser developer's eye. But what was even more surprising about this particular CSS addition, was that it had already been partially implemented in some of the "contemporary" browsers.

Well I think that's probably enough of an introduction... Was I a little over-the-top? :) Anyway the name of this particular little find is what is currently known as the "Flexible Box Layout" or FlexBox for short - however I do get the feeling this may change before it actually becomes more mainstream.

http://www.w3.org/TR/css3-flexbox/

Flexible Box Model?

Now if you aren't up on web-developer terms, you may have no idea what a Box Model is... If that's so then I'm pretty certain you wont loose any sleep if I don't go in to detail here. Sufficed to say, it is the "building block" logic that controls the way HTML Elements are rendered - in terms of their dimensions and positioning. Whilst that might sound extremely uninteresting, you may at least realise that a change to anything described as a "building block" can have fundamental affects.

In my experience there are two rather major layout desires that crop up time and again when web-developing. And the fact that there is no elegant way of implement a solution for either (with current HTML or CSS) it normally leaves only two options. Using a css/js hack... or the complete avoidance of the problem by design.

Find your center

FlexBox totally solves the vertical center problem. You can have any number of child elements positioned to the exact centers of their parents - along the horizontal and the vertical - without the need to know the parent's specific dimensions. Just like the good old days and the "evil" table cell. :)

To Infinity and ...

I have yet to test this, but as far as I can tell FlexBox should do away with the problem of "premature new line collapse" (more correctly known as wrapping) when dealing with horizontally floated elements. Currently (afaik) if you wish to create - for example - a horizontally scrolling website made of out many floated elements. You have to set the sum total width of the floated children on the parent container. Otherwise the elements will just wrap when then hit the side of the viewport.

Now I know there are possibly white-space css properties or even word-wrapping properties that could be used, but these never seem to work 100% cross-browser, and probably shouldn't be used to affect floated elements. Which is why setting the sum total has always been a better solution.. but obviously this involves either having fixed hard-coded content, or some programmatical way of calculating the width for the parent (PHP, JS). FlexBox actually allows you to control the flow of the children which should do away with this annoying problem entirely - just need to state the flow as being constantly horizontal. Obviously I need to research this though.

Collapsible HTML Email and Outlook

by Developer @ pebbl.co.uk

Have you every tried building a pixel perfect HTML email that works across a range of destination user-agents?

If the answer is yes then you'll know just how frustrating it can be - especially when you include the likes of Lotus Notes (6.5.4) and Outlook 2007/2010. If no, then it's probably only a matter of time, but I wish you luck in avoiding the problem... :)

Now usually - at my company - we stick to a rather rigid design ethic in order to minimize the problems we'll undoubtedly encounter when trying to build complex layouts for these archaic systems. But the other day we had a request from a Designer who wanted to create a layout that would work across all the usual agents we support (which already includes mobile devices) - but he also wanted the layout to keep it's readability on mobile devices without the need for zooming.

Obviously this is achievable, but it's only achievable using a collapsible template (something we usually employ for websites) and then only using quite modern (but degradable) standards. This causes two problems for email:

  1. Email is anything but modern - most popular user-agents are at least 5 years old now (if not more).
  2. Pretty much the only way to achieve a pixel perfect design in an email is to use tables - and tables are anything but "collapsible" or "degradable".

If at first...

Undeterred by the possible problems, we set about testing different ideas, and just experimenting with the different user-agents at our disposal. Initially everything was far too easy, we almost "seemed" to have a layout that worked in the first 30 minutes, using only the old HTML Table attribute "align" and using separate tables like "cells".

The following example shows two separate tables aligned using align="left"

Cras dignissim porttitor eros non rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Cras dignissim porttitor eros non rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Now the same code, but forced to wrap at 200px across, as you can see the layout collapses to the parent's width - which is exactly what we needed

Cras dignissim porttitor eros non rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Cras dignissim porttitor eros non rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

So the above handled the collapsing behaviour we needed, but we also required the ability to create "rows" from these left aligned tables. If we just put a whole load of left aligned tables together, they would all wrap along side each other, not really creating the most typical of email layouts... :) So the most obvious way of achieving this was to do what we would normally do in a HTML Email... wrap the aligned tables with another table.

The following shows aligned left tables that would all float in a line, if it weren't for the outer wrapping tables acting as the "row" part of our structure (creating a 2x2 grid).

Cras dignissim porttitor eros non rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Cras dignissim porttitor eros non rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Cras dignissim porttitor eros non rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Cras dignissim porttitor eros non rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

The above all seemed to work perfectly - mainly in Mac Mail, Win/Mac Thunderbird, Internet Explorer, FireFox, Opera, Chrome and Safari and even Entourage (these being the quickest and easiest to test in). But then, as usual, the respective rugs were pulled out from underneath ourselves once we tested the layout in Outlook 2007.

... you don't succeed?

Whoever thought it would be a good idea to use the Microsoft Word WYSIWYG HTML Engine to render HTML Emails in Outlook 2007 and 2010, must have been mad, lazy, lost or ever-so-slightly confused (delete as appropriate). It causes no end of rendering problems for developers, usually with random and inexplicable sizing calculations or padding problems.

Anyway - complaining aside - it took a little while to work out but it seemed there were a few issues befalling the layout in Outlook:

  1. Outlook 2007 automatically shrunk any table with no width set to the smallest width it could have. This obviously affected our outer tables (our "rows") as these only work in a collapsible design if we don't specify a width. This idea had worked in everything save for OL2007 which just ended up auto-collapsing our structure to it's minimum width (rather useless).
  2. Our aligned tables did line up as we'd hoped, but there seemed to be inexplicable horizontal spacing (what a surprise!) of about 10px between each table - pretty much useless for a pixel perfect design.
  3. There also seemed to be about 10px of vertical space between each group of aligned tables (our "rows").

So after the usual trial and erroring we managed to solve the above problems (almost).

  1. Rather than relying on outer tables to create our "rows" we had to remove any non-dimensioned tables. Instead we used a more web page based approach by using a page break with clearing set to "all" (the value of "both" failed to work in OL2007). This introduced another problem, yet more vertical space between each "row". This was fixed by wrapping the <br clear="all"> tag with a <font> tag with it's size and line-height all set to 0.