-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.

1
  • 3
    window.location.hash - and no jQuery at all Commented Nov 12, 2014 at 15:42

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

Comments

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 
 }

Comments

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
}

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.