what is the # symbol in the url

后端 未结 5 954
失恋的感觉
失恋的感觉 2020-12-10 13:28

I went to some photo sharing site, so when I click the photo, it direct me to a url like

www.example.com/photoshare.php?photoid=1234445

. and w

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 13:53

    The '#' symbol in the context of a url (and other things) is called a hash, what comes after the hash is called a fragment. Using JavaScript you can access the fragment and use its contents.

    For example most browsers implement a onhashchange event, which fires when the hash changes. Using JavaScript you can also access the hash from location.hash. For example, with a url like http://something.com#somethingelse

    var frag = location.hash.substr(1);
    console.log(frag);
    

    This would print 'somethingelse' to the console. If we didn't use substr to remove the first character, it frag would be: '#somethingelse'.

    Also, when you navigate to a URL with a hashtag, the browser will try and scroll down to an element which has an id corresponding to the fragment.

    http://en.wikipedia.org/wiki/Fragment_identifier

提交回复
热议问题