-1

I have somes links like this:

mywebsite.com/tutos.php?name=tuto_name#comments
mywebsite.com/tutos.php?name=tuto_name#download

My question: how to get the text after the #.

thanks.

MH2K9
  • 11,951
  • 7
  • 32
  • 49
Said Hamri
  • 36
  • 2

4 Answers4

2

window.location.hash is a cross browser solution that returns the value (including the hash)

You can remove the hash by doing:

var hash = window.location.hash.substr(1);
Jordan Burnett
  • 1,799
  • 8
  • 11
0

You can use window.location.hash. It takes with # (ie, #comments). To remove trialing # use .substring(1). Example:

var str = window.location.hash.substring(1);
alert(str);
MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

I use the following JS function that will do this:

function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)','i').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null
}
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
0

I use the following as it not only grabs the hash value (without the hash itself (taking the 2nd part (array[1] of the split)), but also tests the undefined case as well which can cause problems in some cases.

var hashVal = window.location.hash.split("#")[1];
if( hashVal && hashVal != "undefined" ) {
       //work it 
 }
BReal14
  • 1,603
  • 1
  • 12
  • 35