How can you check for a #hash in a URL using JavaScript?

前端 未结 19 2792
说谎
说谎 2020-11-22 02:10

I have some jQuery/JavaScript code that I want to run only when there is a hash (#) anchor link in a URL. How can you check for this character using JavaScript?

19条回答
  •  遇见更好的自我
    2020-11-22 02:39

    sometimes you get the full query string such as "#anchorlink?firstname=mark"

    this is my script to get the hash value:

    var hashId = window.location.hash;
    hashId = hashId.match(/#[^?&\/]*/g);
    
    returns -> #anchorlink
    

提交回复
热议问题