removing the # from window.location.hash

Viewed 72648

I have this simple script:

$(document).ready(function(){

var $yoyo = window.location.hash;

alert($yoyo);

});

But I need to get rid of the # symbol as I'll be using the variable to locate div ids. I've tried using .remove('#') but that doesn't seem to be working.

Many thanks in advance!

4 Answers

For those who may not have read the lea verou blog shared by Steve Harrison, a version with 4 less bytes and using newer JS definitions would be:

let $yoyo = window.location.hash.slice(1)

Slice is an array method that when given one index returns the values from the starting index to the last index. Since strings in Javascript are considered an array of characters and the location hash will always have a starting # or be an empty string, this works.

http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/

Related