什么是URL中的#符号 [英] what is the # symbol in the url

查看:204
本文介绍了什么是URL中的#符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我去了一些照片分享网站,所以当我点击照片时,它会指引我到像



www.example这样的网址。 com / photoshare.php?photoid = 1234445



。当我点击这个页面中的其他照片时,网址变成了

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



如果我点击同一页面中的其他照片,则#后面的唯一数字会发生变化。和美丽的照片一样,像

www.example.com/photoshare.php?album=holiday#!prettyPhoto [gallery2] / 2 /



。我假设他们使用ajax,因为整个页面似乎没有加载,但url被更改。


<在之后的一部分URL(包括和)是解析方案 //en.wikipedia.org/wiki/Fragment_identifierrel =noreferrer>片段标识符。它从URL的其余部分是特殊的。要记住的关键是仅客户端(当然,客户可以选择将它发送到服务器......只是不作为片段标识符):


片段标识符的功能与URI的其余部分不同:它的处理仅仅是客户端,没有来自服务器的参与 - 当然,服务器通常有助于确定MIME类型,并且MIME类型确定片段的处理。当代理(如Web浏览器)从Web服务器请求资源时,代理会将URI发送到服务器,但不会发送片段。代之以代理等待服务器发送资源,然后代理根据文档类型和片段值处理资源。

这可以用于导航到锚点链接,如: http://en.wikipedia.org / wiki / Fragment_identifier#Basics (注意它是如何进入基础部分的)。



过去这只是在过去的锚点 ,它现在用于在许多由JavaScript支持的站点中存储可导航状态 - 例如,gmail使用它。而且,就像这里的情况一样,有一些照片共享JavaScript也使用片段标识符来进行状态/导航。



因此,正如所怀疑的那样,JavaScript 捕获片段(有时称为散列)更改并执行AJAX(或其他后台任务)来更新页面。当片段发生变化时,页面本身不会重新加载,因为URL仍然指向相同的服务器资源(片段标识符之前的URL部分)。

新浏览器支持 onhashchange 事件,但通过各种轮询技术支持监视很长时间。



愉快的编码。


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 when I click the other photo in this page the url become

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

and if I click other photos in the same page, the only the number behind # changes. Same as the pretty photo like

www.example.com/photoshare.php?album=holiday#!prettyPhoto[gallery2]/2/

.I assume they used ajax because the whole page seems not loaded, but the url is changed.

解决方案

The portion of a URL (including and) following the # is the fragment identifier. It is special from the rest of the URL. The key to remember is "client-side only" (of course, a client could choose to send it to the server ... just not as a fragment identifier):

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the server — of course the server typically helps to determine the MIME type, and the MIME type determines the processing of fragments. When an agent (such as a Web browser) requests a resource from a Web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the document type and fragment value.

This can be used to navigate to "anchor" links, like: http://en.wikipedia.org/wiki/Fragment_identifier#Basics (note how it goes the "Basics" section).

While this used to just go to "anchors" in the past, it is now used to store navigatable state in many JavaScript-powered sites -- gmail makes heavy use of it, for instance. And, as is the case here, there is some "photoshare" JavaScript that also makes use of the fragment identifier for state/navigation.

Thus, as suspected, the JavaScript "captures" the fragment (sometimes called "hash") changing and performs AJAX (or other background task) to update the page. The page itself is not reloaded when the fragment changes because the URL still refers to the same server resource (the part of the URL before the fragment identifier).

Newer browsers support the onhashchange event but monitoring has been supported for a long time by various polling techniques.

Happy coding.

这篇关于什么是URL中的#符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆