这是不可能的,因为客户端不会将“锚定部分”发送到服务器
举个例子,以下是提交http://example.com/#foobar(使用Wireshark记录)后Chrome生成的确切要求:
GET / HTTP/1.1
Host: example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/532.3 (KHTML,like Gecko) Chrome/4.0.223.11 Safari/532.3
Cache-Control: max-age=0
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
If-None-Match: "b300b4-1b6-4059a80bfd280"
If-Modified-Since: Tue,15 Nov 2005 13:24:10 GMT
请参见no #foobar.所以服务器应用程序无法读取它.
你可以做一些JavaScript的魔术,将锚点存储在cookie或隐藏的输入域中,或者是你进入的任何巫术.但是,对于不是源自您自己的站点的请求,它不会有效.在查询字符串的服务器部分进行所需的任何操作都比较简单,并且仅使用锚定到JavaScript的任务,或者使用它来在一个简单的HTML文档中导航,但这就是90s;).
以下是RFC 1808的重要部分:
Note that the fragment identifier (and the “#” that precedes it) is not considered part of the URL. However,since it is commonly used within the same string context as a URL,a parser must be able to recognize the fragment when it is present and set it aside as part of the parsing process.