25

I'm trying to get the hash value after a link was clicked. Any ideas?

e.g.

The Link

index.html#needThis

This is my result:

index.htmlneedThis

How can I remove the 'index.html' ?

$('#myselector').on('click', 'a', function(){
    var hash = $(this).attr('href').replace('#', '') //get url
    console.log(hash);
})
2
  • Possible duplicate of stackoverflow.com/questions/1822598/…. You don't need to 'remove' the index.html, you need to grab only the hash. Commented Nov 12, 2012 at 13:08
  • Why al all answers using $(this).attr('href')?! Use native JS where possible: this.href has the exact same value, withouth the need for another library or function calls. Commented Aug 25, 2016 at 10:27

5 Answers 5

71

update

Modern browsers (not sure how back it goes) can extract the hash directly from anchor elements (so i added #5)


Take your pick ..

  1. var hash = $(this).attr('href').split('#')[1];
  2. var hash = $(this).attr('href').replace(/^.*?#/,'');
  3. var href = $(this).attr('href'), hash = href.substr(href.indexOf('#')+1);
  4. var hash = $(this).attr('href').match(/#(.*$)/)[1];
  5. var hash = this.hash.substr(1);

update

Just revisited this and i believe that #2 can be improved to

$(this).attr('href').replace(/^.*?(#|$)/,'');

This way if no hash exists it will return empty instead of the whole url..

Sign up to request clarification or add additional context in comments.

4 Comments

The second is the perfect solution! No Exception : var hash = $(this).attr('href').replace(/^.*?#/,''); thank you @Gaby
@gaby what is test in the 3. solution?
@TobiasOberrauch, great eyes... I have corrected it ... (it was supposed to be a temporary reference to the href string)
@Bellash just posted an improvement to it :)
16

Just use var hash = window.location.hash.

It returns everything after #

Or if you've got a variable which contains an url:

$(this).attr('href').substring($(this).attr('href').indexOf('#'))

3 Comments

Thank you for that but it's not working in my case because I need the href of the a-element which is triggered and not from the current url. :)
in that case use a.substring(a.indexOf('#'))
window.location.hash doesn't actually return everything after #, but everything starting from #, so # character is included.
5

You can try with window location hash.

$('#myselector').on('click', 'a', function(){
    var hash = $(this).attr('href').replace('#', '') //get url
    var hash2 = window.location.hash ;
    console.log(hash2);
})

1 Comment

Thank you but the same problem as above.
2

Why not simply use:

window.location.hash

Works just fine for me.

Comments

1

Try this,

$('#myselector').on('click', 'a', function(){
    var url = $(this).attr('href') //get url
    var arr = url.split('#');
    alert(arr[0]); //URL without hash #
    var hash = arr[1];
    console.log(hash);
})​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.