Thursday, 2 September 2010

Seperate and Different

I though I needed to split the functionality of some similar pages depending upon the name of the page... I should explain better! I have some pages that all do pretty much the same thing on a given set of data... but not all of the pages react in the same way to events. So I got to thinking about how I could branch the code depending upon which page was calling the JavaScript, so then I got to thinking about how to find the name of the page and came across this code:

var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

But then that got far too complex and the JavaScript kept getting bigger and bigger and uglier and uglier so instead I had a different version of the same basic file for each page... but that got far to complicated in terms of remembering what I'd updated and where as I was developing... so I got the thinking about PHPs include() but JavaScript doesn't have one, it does however have fantastic DOM support so, thanks to a little research, I found this code from the really rather excellent Stephen Chapman. Now I've still got the separate JavaScript files for each page - but these pages are far smaller and concentrate on the differences rather than having to keep abreast of the similarities - but all the common functionality is kept in a separate file so that I don't need to hunt down each function in each file and update with the latest version. Cool ehh?

Wednesday, 1 September 2010

createLink(text, uri, img, alt)

So I've been working on a breadcrumb navigation where I'm using JavaScript to generate a number of CSS styled HTML list elements. Each of these elements are made up of a text element and an image so that in terms of the DOM they look like this:

<li>
  <a href="someUrl.html">
    someText <img src="someImageLocation.png" alt="someAlternativeText"/>
  </a>
</li>

There were 3 of these links and the function was getting longer and longer, so I got to thinking about having to repeat so much redundant code so I pulled the 3 list items out of the function and created another function:

function createLink(text, uri, img, alt){
  var li = document.createElement("li");
  var a = document.createElement("a");
  a.setAttribute("href", uri);
  var liText = document.createTextNode(text);
  var liImg = document.createElement("img");
  liImg.setAttribute("src", img);
  liImg.setAttribute("alt", alt);
  a.appendChild(liImg);
  a.appendChild(document.createTextNode(" "));
  a.appendChild(liText);
  li.appendChild(a);
  return li;
}

Then I called that 3 times thus:

breadCrumb.appendChild(createLink("someText", "someUrl.html", "someImageLocation.png", "someAlternativeText"));

And Bob's your Uncle and Fanny's your...

Tuesday, 31 August 2010

beating the game

According to this post on the googlereader blog I'm beaten the game by reading more than 300,000 posts. YAY!

Older Men Scam

(Thanks to this post on uk.rec.waterways - I'm pretty sure that this has made it's way across the pond but I found it funny ;-))

Women often receive warnings about protecting themselves at the mall and in dark parking lots, etc. This is the first warning I have seen for men.

A 'heads up' for men who may be regular customers at Sainsburys, Tesco, Costco, or even Asda. A man can become a victim of a clever scam while out shopping. Simply going out to get supplies has turned out to be quite traumatic.

Here's how the scam works:

  • Two nice-looking, college-aged girls will come over to your car or truck as you are packing your purchases into your vehicle. Both start wiping your windshield with a rag and squeegee, with their breasts almost falling out of their skimpy T-shirts.
  • When you thank them and offer them a tip, they say 'No' but instead ask for a ride to McDonald's.
  • You agree and they climb into the vehicle. On the way, they start undressing. Then one starts crawling all over you, while the other steals your wallet.

I had my wallet stolen May 4th, 9th, 10th, twice on the 15th, 17th, 20th, 24th, & 29th. Also June 1st & 4th, twice on the 8th, 16th, 23rd, 26th & 27th, and very likely again this upcoming weekend.

So tell your friends to be careful. What a horrible way to take advantage of us older men. Warn your friends to be vigilant. Asda has wallets on sale for £2.99 each. I found even cheaper ones for £0.99 at the pound shop. Also, you never get to eat at McDonald's. I've already lost 11 pounds just running back and forth from Sainsburys, to Tesco, to Asda, Etc.

Please send this on to all the older men that you know and warn them to be on the lookout for this scam. (The best times are just before lunch and around 4:30 in the afternoon.)

JavaScript trim functions

These are from: Shailesh N. Humbad, and they are really rather cool, check out his site.

function trim(stringToTrim) {
  return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
  return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
  return stringToTrim.replace(/\s+$/,"");
}

arrayValueExistsAdapted(anArray, aValue) JavaScript

/**
 * Tests to see if aValue exists in anArray. Doesn't check to see if the whole of aValue exists but instead looks at the 
 *   string which comes before a colon punctuation mark (":") within each string element within the array.
 * @param anArray - an string array which is parsed, the element which is tested is anything before a colon in each 
 *   element of the array.
 * @param aValue - a value which is compared to the part of the element of anArray which comes before the colon 
 *   punctuation mark.
 * returns true if aValue is found within anArray, else return false. 
 */
function arrayValueExistsAdapted(anArray, aValue){
  var found = false;
  for (var i = 0; i < anArray.length; i++){
    var crumb1 = anArray[i].split(":");
    if (crumb1[0] == aValue){
      found = true;
    }
  }
  return found;
}

Adapted from arrayValueExists(anArray, aValue):

/**
 * Test to see if aValue is present within the elements of anArray, returns true if it does, 
 *   else returns false.
 * @param anArray
 * @param aValue
 */
function arrayValueExists(anArray, aValue){
  var found = false;
  for (var i = 0; i < anArray.length; i++){
    if (anArray[i] == aValue){
      found = true;
    }
  }
  return found;
}

valueToInt(value, up) JavaScript

/**
 * Function which accepts two parameters.
 * @param value - value passed into the function either a float, int or string (containing a number).
 * @param up - boolean value which defines whether returned integer should be rounded rounded up or rounded down
 * 
 * If "up" isn't present then the number is rounded, if "up" is true then the number is rounded up 
 * else it's rounded down.
 */
function valueToInt(value, up){
  var returnValue;
  // From: http://joeyjavas.com/2007/06/25/javascript-how-to-remove-all-commas-from-a-number/
  // If we have a number that's in the format 123,456.00 this removes the commas and gives us 123456.00 
  // which can subsequently be parsed as a float.
  if(typeof(value) == "string"){
    value = value.replace(/\,/g,'');
    try{
      returnValue = parseFloat(value)
    }catch(e){
      returnValue = 0;
    }
  }
  if(typeof(value) == "number"){
    returnValue = parseFloat(returnValue)
  }
  if(!up){
    returnValue = parseInt(Math.round(returnValue));
  }else{
    if(typeof(up) == "boolean"){
      if(up){
        returnValue = parseInt(Math.ceil(value));
      }else{
        returnValue = parseInt(Math.floor(value));
      }
    }else{
      returnValue = parseInt(Math.round(returnValue));
    }
  }
  return returnValue;
}

And to add them back:

function commaFormat(amount){
  var number = '' + amount;

  if (number.length > 3) {
    var mod = number.length % 3;

    var output = (mod > 0 ? (number.substring(0,mod)) : '');

    for (i=0 ; i < Math.floor(number.length / 3); i++) {

      if ((mod == 0) && (i == 0)){

        output += number.substring(mod+ 3 * i, mod + 3 * i + 3);

      }else{
        output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);

      }
    }
    return (output);
  }

  else return number;
}

As per CodeColorizer or here.