SUBSCRIBE VIA RSS


Subscribe to our feed

Status Updates

  • First iPhone app nearly complete! I've been waiting for 3 weeks for Apple to finish "conducting company identity verification."
    3 days ago
  • Rainy day! Staying indoors. Creating computerized players in a simulation game that models cooperation in the work place.
    75 days ago
  • Up at 8AM, with a hot cup of coffee brought back from Cafe Lola in Ann Arbor. Will I succeed in putting in 8 billable hours in one day?
    101 days ago
  • full scale irish band outside my window playing the pipes for the past 20 minutes. pretty fun.
    109 days ago
  • listening to Kristin on Maxim Radio!
    115 days ago
  • back from an amazing meal at ouest with even more amazing company.
    116 days ago
  • wondering how so many online tshirt companies stay in business--ones that sell shirts with funny messages, i never see them in real life
    122 days ago

Topics

TWITTER

The old fashioned way

RECENT TUNES

November 21, 2008 – 5:39pm Javascript get window hash/anchor, get link target

There are two functions I find myself using all the time. They are very useful for javascript events that read the link href attribute to determine an action’s target.

function getHash() {
  var hash = window.location.hash;
  return hash.substring(1); // remove #
}
 
function getLinkTarget(link) {
  return link.href.substring(link.href.indexOf('#')+1);
}
Posted in  Uncategorized   |     |  delicious  Digg

2 Responses to “Javascript get window hash/anchor, get link target”

  1. I’d use a regexp for the second, but those are very useful.

    By Jon Williams on Nov 21, 2008

  2. Nice work. Sometimes I use question marks after my hash to specify actions:

    http://a.com#favorites?recent

    So this code is a useful extension of your function (useful to me at least!):


    function getHash(excludeQuestionMark) {
    if (typeof excludeQuestionMark == 'undefined') var excludeQuestionMark = true;
    var hash = window.location.hash;
    var stop = hash.indexOf('?');
    // if there was no question mark in the hash, or if excludeQuestionMark is set to false
    if (stop == -1 || !excludeQuestionMark)
    var returner = hash.substring(1); // remove #
    else
    var returner = hash.substring(1,stop); // remove # and exlude everything after '?'
    return returner;
    }

    By Mani Tadayon on Jan 5, 2009

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word