I really need to think of a clever name for my blog

Code samples, geeky links and other musings. Note this blog has recently moved from Posterous, so isn't fully back up to speed yet!

Free Javascript books online

http://jsbooks.revolunet.com/

Dirtiest. Hack. Ever. iOS Event Delegation Solved(?)

So, iOS Safari has a problem with javascript event delegation.

Normally you click (or trigger any event) on an element, and the browser traverses back up the dom, triggering that event on each parent. Thus we can attach an event listener to a ul, and all it li’l children (and bastard offspring) will inherit the same listener.

Possibly for memory management reasons, this doesn’t happen in iOS Safari – unless the element is a link or input – an actual ‘clickable’ element. So there is some logic here, but we’re in a different age and this kind of behaviour is completely at odds with all other browsers, which screws us pretty bad as so much of our application behaviour today relies on event delegation.

There is a fix – if Safari thinks you want your element to be interactive, it will allow delegation – give the element the css property of cursor:pointer;

Ok, fine if you have a specific element which needs delegation, but what if you have an element with lots of children? What a waste, adding the cursor property to all that CSS.

And what if you need to do something very generic? We need to emulate rollover behaviour with touch sometimes, so we want to touch open a menu, and close on touch off. We can’t possibly know which element a user will touch off on. If a modal effect is desired, that’s easy – our modal layer is a single element we can add the cursor to. But if we want a touch off action to be able to trigger another element, as well as trigger our mouseout-like behaviour, that’s no good.

Greedy Bastard Ahoy!

Obviously we can’t simply give cursor:pointer; to everything, I mean, the cursor MEANS something.

Or can we? There IS no cursor on iDevices. All we need do is determine when we are on a device, say with Modernizr [new window], and away we go…

.touch *{    cursor:pointer;}

Dirty. Greedy. Lazy. Bastard.
But it works.
Now, touch isn’t a perfect answer here – there are touch devices that support input peripherals, and a touch PC with a mouse would be a bit borked, as they would have a constant pointer – but there are refinements. User Agent sniffing is a bit dodgy, but c’mon, iOS users changing their user agent? And frankly, if you change your user agent, you should expect things to behave oddly.

https://gist.github.com/855078

Of course, if memory usage was SUCH a problem that Apple engineers simply HAD to disable event delegation in the first place, then we have just reopened that can of worms – into our pants, before diving into a piranha pool – so it is probably best to excercise caution…

Exciting me today… Social media jQuery, one element icons and more…

Hell yeah… Dealing with social media counters and buttons is TEDIOUS.

http://filamentgroup.com/lab/socialcount/

I love clever CSS. one-div aims to build a database of single div icons leveraging pseudo elements and some cunning gradients – dig around in the css to learn some interesting tricks. Will be fascinating to see how much more cunning people’s techniques will become over time…

http://one-div.com/

If you have a heavily AJAX’ed web app, telling the user’s they are offline (a la Gmail) would be a great boon, n’est pas?

http://oskarkrawczyk.github.com/heyoffline.js/

Automated jQuery testing? Sounds interesting…

https://github.com/ianb/walkabout.js

Scrubby – scrub slider style adjustment of JS values. Fantastic for experimenting, damn handy for interfaces…

http://nornagon.github.com/scrubby/

All above scraped from the often very useful Co-drops Collective newsletter
http://tympanus.net/codrops/collective/collective-40

 

Learn about the HTML 5 Audio API from repropductions of the BBC Radiophonics workshop. Sounds pretty cool to me…

http://webaudio.prototyping.bbc.co.uk/

 

I also mean to get round to reading this at some point:

http://net.tutsplus.com/tutorials/javascript-ajax/what-they-didnt-tell-you-about-es5s-array-extras/

 

Prism – JS/CSS Syntax Highlighting

Looks great.

Well, the dark theme is a bit, shall we say, chocolate-y, and the funky theme is a horrific 80′s headfuck (if slightly amusing), but hey, CSS is CSS so you can change that. The default theme is perfectly tasteful. More importantly it’s lightweight, fast and extensible. I should add it to this blog sometime.

http://prismjs.com/

#BADA55 !important – Chrome Dev Tools

Freaking awesome developments in Chrome Dev Tools – putting FireBug on notice maybe?

From Paul Irish’s blog:

Pavel Feldman (Dev Tools engineering lead) and I just spoke at Google I/O about the Chrome Developer tools.

We covered a whole lot of enhancements to style manipulation, timeline inspection, script debugging, DOM and Event listener breakpoints.

We announced a few brand new features I’m really jazzed about:

For full text and comments got to
http://paulirish.com/2011/a-re-introduction-to-the-chrome-developer-tools/

For reference: SASS introduction and X-browser jQ plugin testing

Recent posts for later reading:

Still not had a specific need from a client to use SASS or LESS – which is a shame as I’m keen to get into them, but not had the time and incentive. OK, it’s my own lazy ass fault really!
This is a basic SASS intro:

http://blog.teamtreehouse.com/the-absolute-beginners-guide-to-sass

jQ2.0 will be dropping <IE9 support. Good for them! But… clients will still request OldIE support.

http://markdalgleish.com/2013/01/testing-jquery-plugins-cross-version-with-gr… 

Visual Event Bindings Tool

Ah, this is SO good!

A simple bookmarklet to identify events bound to elements – and where multiple elements prevent you viewing the one you want, you can simply hide the events for individual elements.

http://www.sprymedia.co.uk/article/Visual+Event+2

This is fantastic. The only problem is, considering this is Version 2, how can it be that I never found it before? 

Bookmarklets are a bit tricky to use when debugging devices however – some suggestions:
http://www.plantoeat.com/help/kb/mobile-site/use-the-bookmarklet-on-a-mobile-… 

Literary Javascript

A most amusing post comparing the theoretical code authoring styles of famous authors.

function theSeriesOfFIBONACCI(theSize) {  //a CALCKULATION in two acts.  //employ'ng the humourous logick of JAVA-SCRIPTE  //Dramatis Personae  var theResult; //an ARRAY to contain THE NUMBERS  var theCounter; //a NUMBER, serv'nt to the FOR LOOP  //ACT I: in which a ZERO is added for INITIATION  //[ENTER: theResult]  //Upon the noble list bestow a zero  var theResult = [0];  //ACT II: a LOOP in which the final TWO NUMBERS are QUEREED and SUMM'D  //[ENTER: theCounter]  //Commence at one and venture o'er the numbers  for (theCounter = 1; theCounter < theSize; theCounter++) {    //By divination set adjoining members    theResult[theCounter] = (theResult[theCounter-1]||1) + theResult[Math.max(0, theCounter-2)];  }  //'Tis done, and here's the answer.  return theResult;  //[Exuent]}

http://byfat.xxx/if-hemingway-wrote-javascript

jQuery .width() may not return pixels if element is hidden

jQuery 1.7.1

This got me for a while – while there is some discussion of .width() not behaving properly, only this stackOverflow [new window] hinted at the fact I wasn’t mad – all the others being sparse on examples, or poorly worded, leaving me wondering.

But, hell, I’ll give it a go, I thought, and it turned out that I was suffering exactly this:

The jQuery documentation clearly states:

The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example,400px). The .width() method is recommended when an element’s width needs to be used in a mathematical calculation.

Ok, fair enough.

But I had {n} columns in a graph, containing a bar in each. The columns are set to (100 / {n})% width while the graph is being populated – and most importantly here, hidden.

I wanted to give my bars a little padding in pixels, so I needed to get the column size in pixels. In my example I had 4 columns, width 25% each, which worked out as 88px. Calling $(‘.column).width() -6 I expected to get 82, but got 19 instead.

One stackOverflow suggested that you should use an id instead – not ideal, as I don’t always know what the ids will be and don’t want to bother storing one just for the sake of the calculation. But I tried it with this case as I did know the ids – and it still didn’t work.

So I moved the column .width() call to after my .show() call – and magically, it returned 82.

Call it an ‘undocumented feature’, call it a bug, but watch out for it!

I ought to report this, but as of writing the jQuery trac is down. MySQL has gone away apparently :’(

Gavick Pro ‘GK Gamebox’ Joomla Template – popup login disappears when clicking on username or password input in Chrome

Symptom:

In Google Chrome, when using the popup login form accessed from the login link at the top of the template, if you click on the username or password inputs, the popup disappears.

Cause:

The mootools script is designed so that when a user clicks out of the DOM object popup_login, it fades out. This is done with any general document click IF the mouse is out of popup_login. This is determined by checking a variable set by mouseenter and mouseleave.

In Chrome, mouseleave is triggered when you enter child elements – not our desired functionality!

Solution:

templatesgk_gameboxjsgk.script.js 

130a131> <span style="white-space: pre;"> </span>$('popup_login').getElements('input').addEvent('mouseenter',function(){login_over = true;});

(Unix diff formatting)