Comments

Loving the DOM; insert child nodes at any position

Posted by sh1mmer on Dec 11, 2006 in JavaScript

One day I was bitching about the lack of a way to insert something at an arbitrary point into a list of DOM nodes. "I don't want to walk the DOM, or walk with Dinosaurs, for that matter!" said I, or something equally witty. Steve Webster sick of my whining, no doubt, suggested I write one.

So I did (with a little help from Heilmann).

Here it is in all it's glory

JavaScript:
  1. insertAtPosition = function(root, el, pos) {
  2.  //remove any whitespace nodes
  3.  for(var i=0,j=root.childNodes.length;i<j ;i++){
  4.     if(x.childNodes[i]===undefined){break;}
  5.     if(x.childNodes[i].nodeType===3){
  6.         x.removeChild(x.childNodes[i]);
  7.         i--;
  8.     }
  9.  }
  10.  //if the position is out of the current scope of the element
  11.  if(pos>root.childNodes.length || pos<0) {
  12.  return false;
  13.  }
  14.  // if pos is the same as length then add to the end of the children array
  15.  if (pos===root.childNodes.length) {
  16.  root.appendChild(el);
  17.  }
  18.  // insert before works for all other cases
  19.  else {
  20.  root.insertBefore(el, root.childNodes[pos]);
  21.  }
  22.  return true;
  23. };

 
Comments

London cyclist, dangers to themselves (or just me)

Posted by sh1mmer on Dec 11, 2006 in General

So, I love the idea of cycling, especially in a city like London. It really is a great way to get around while respecting the environment and getting a bit of exercise.

However, it seems like a lot of London cyclists have inherited the worst parts of London drivers and London pedestrians. What I mean by this is that cyclists not only believe that the rules of the road don't apply to them (as London pedestrians certainly have never heeded them) but they also believe they own the road in the fashion of a London driver.

What this creates is a person who will ride the wrong way up a one way street expecting pedestrians not to cross in front of them, even if they are not looking or paying attention. Some of them even do this while wearing headphones!

While this does pose a bit of a threat to me, the pedestrian, I wonder how many cyclists get dragged under buses because of their cavalier attitude.

Copyright © 2012 Kid666 Blog All rights reserved. Base theme by Laptop Geek.