javascript 为什么 URL 的哈希部分在服务器端不可用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3664257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is the hash part of the URL not available on the server side?
提问by CRISHK Corporation
For example if I type in the URL:
例如,如果我输入 URL:
http://www.foo.com/page.php?parameter=kickme#MOREURL
http://www.foo.com/page.php?parameter=kickme#MOREURL
Then on the server there is no part: #MOREURL
然后在服务器上没有部分:#MOREURL
Is possible to send or get these part to the server without jQuery AJAX?.
是否可以在没有 jQuery AJAX 的情况下将这些部分发送或获取到服务器?
回答by shamittomar
No, it is available to the browser only, so you have to deal it with Javascript. The server can not read it.
不,它仅对浏览器可用,因此您必须使用 Javascript 来处理它。服务器无法读取它。
Explanation:
Basically the hash component of the page URL (the part following the # sign) is processed by the browser only - the browser never passes it to the server. This sadly is part of the HTML standard and is the same whether or not you are using IE or any other browser (and for that matter PHP or any other server side technology).
说明:
基本上页面 URL 的散列组件(# 符号后面的部分)仅由浏览器处理 - 浏览器从不将其传递给服务器。遗憾的是,这是 HTML 标准的一部分,无论您使用的是 IE 还是任何其他浏览器(就此而言,PHP 或任何其他服务器端技术)都是一样的。
Here's what Wikipediasays about it:
以下是维基百科对此的描述:
The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the server. 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 fragment value. In the most common case, the agent scrolls a Web page down to the anchor element which has an attribute string equal to the fragment value. Other client behaviors are possible
片段标识符的功能与 URI 的其余部分不同:即,它的处理完全在客户端进行,没有服务器的参与。当代理(例如 Web 浏览器)从 Web 服务器请求资源时,代理将 URI 发送到服务器,但不发送片段。而是代理等待服务器发送资源,然后代理根据分片值对资源进行处理。在最常见的情况下,代理将网页向下滚动到具有等于片段值的属性字符串的锚元素。其他客户端行为是可能的
回答by meder omuraliev
http://tools.ietf.org/html/rfc2396#section-4
http://tools.ietf.org/html/rfc2396#section-4
When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch ("#") character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.
当使用 URI 引用对标识的资源执行检索操作时,可选的片段标识符与 URI 用交叉线(“#”)字符分隔,由检索后由用户代理解释的附加引用信息组成行动已成功完成。因此,它不是 URI 的一部分,但经常与 URI 结合使用。
回答by katomaso
I would like to extend the answer on the reason WHYthe fragment is not sent to the server. Because it is intentional and desired behavior. Let's look at URL string in whole.
我想延长的原因,答案为什么片段不被发送到服务器。因为这是有意的和期望的行为。让我们看看整个 URL 字符串。
/path/to/element?query=string&for=server#?optional=fragment&for=browser
<----- URI ----> <---- QUERY STRING ---> <----- FRAGMENT STRING ------>
/path/to/element?query=string&for=server#?optional=fragment&for=browser
<----- URI ----> <---- QUERY STRING ---> <----- FRAGMENT STRING ------>
URIuniquely specifies resource fetched from a server
URI唯一指定从服务器获取的资源
QUERYdefines operations to be performed by the server on the resource
QUERY定义服务器对资源执行的操作
FRAGMENTcontrols browser (application) behavior. Fragment should be used to store application state which should be visible to the user so the user can send link to another user to get the same application state.
FRAGMENT控制浏览器(应用程序)行为。Fragment 应该用于存储应该对用户可见的应用程序状态,以便用户可以将链接发送给另一个用户以获得相同的应用程序状态。
Fragment is the only part of URL free for you to transparently implement single-page web applications (which can run offline on your mobile phone for example). Therefore it must not be sent to the server.
Fragment 是 URL free 的唯一部分,可以让您透明地实现单页 Web 应用程序(例如可以在您的手机上离线运行)。 因此它不能被发送到服务器。
回答by JSAddict
As the browser does not send the hash to the server by default, the only way to do it is to use some Javascript:
由于浏览器默认情况下不会将哈希发送到服务器,因此唯一的方法是使用一些 Javascript:
When the form submits, grab the hash (window.location.hash) and store it in a server-side hidden input field Put this in a DIV with an id of "urlhash" so we can find it easily later.
On the serveryou can use this value if you need to do something with it. You can even change it if you need to.
On page load on the client, check the value of this this hidden field. You will want to find it by the DIV it is contained in as the auto-generated ID won't be known. Yes, you could do some trickery here with .ClientID but we found it simpler to just use the wrapper DIV as it allows all this Javascript to live in an external file and be used in a generic fashion.
If the hidden input field has a valid value, set that as the URL hash (window.locaion.hash again) and/or perform other actions.
当表单提交时,获取哈希值 (window.location.hash) 并将其存储在服务器端隐藏的输入字段中。将其放入 ID 为“urlhash”的 DIV 中,以便我们以后可以轻松找到它。
在服务器上,如果您需要对其进行处理,您可以使用该值。如果需要,您甚至可以更改它。
在客户端页面加载时,检查此隐藏字段的值。您将希望通过它包含的 DIV 找到它,因为自动生成的 ID 是未知的。是的,您可以在这里使用 .ClientID 做一些诡计,但我们发现仅使用包装器 DIV 更简单,因为它允许所有这些 Javascript 存在于外部文件中并以通用方式使用。
如果隐藏输入字段具有有效值,请将其设置为 URL 哈希(再次是 window.locaion.hash)和/或执行其他操作。
We used jQuery to simplify the selecting of the field, etc... all in all it ends up being a few jQuery calls, one to save the value, and another to restore it.
我们使用 jQuery 来简化字段的选择等......总而言之,它最终成为了几次 jQuery 调用,一个是保存值,另一个是恢复它。
Before submit:
提交前:
$("form").submit(function() {
$("input", "#urlhash").val(window.location.hash);
});
On page load:
在页面加载时:
var hashVal = $("input", "#urlhash").val();
if (IsHashValid(hashVal)) {
window.location.hash = hashVal;
}
IsHashValid() can check for "undefined" or other things you don't want to handle.
IsHashValid() 可以检查“未定义”或其他您不想处理的事情。
Also, make sure you use $(document).ready() appropriately, of course.
另外,当然要确保适当地使用 $(document).ready() 。
回答by Mudit Jha
The hash component is not passed on to the server but it is extensively used on the client side. Specifically, in single page applications, the text following a hash is used to represent state of the applicationas different routes. Thus, what happens is: following an initial request to the server which serves the 'home' page along with additional js files which include client-side routing logic such as the router, whenever the user navigates anywhere on the page by clicking an anchor tag, only the part of the URL following the hash component is changed. This prevents a GET request to the server and in response to this 'onhashchange' event, the content of the single page application can be updated depending on the exact route.
散列组件不会传递到服务器,但它广泛用于客户端。具体来说,在单页应用程序中,哈希后面的文本用于将应用程序的状态表示为不同的路由。因此,发生的情况是:当用户通过单击锚标记在页面上的任何位置导航时,遵循对“主页”页面以及包括客户端路由逻辑(如路由器)的附加 js 文件的初始请求,仅更改哈希组件后面的 URL 部分。这可以防止向服务器发出 GET 请求,并且响应此 'onhashchange' 事件,可以根据确切的路由更新单页应用程序的内容。

