简体   繁体   English

从URI获取锚点

[英]Get anchor from URI

I'm writing a JSP/Servlet and I'm trying to get the the anchor part of the URI eg: 我正在编写JSP / Servlet,我正在尝试获取URI的锚点部分,例如:

blabla.rdf#mark

How do I get my mark from my request? 如何从我的请求中获得我的标记? Obviously request.getParameter() doesn't work? 显然request.getParameter()不起作用?

Any help would be welcome. 欢迎任何帮助。

This isn't possible as clients don't send the "anchor part" to the server 这是不可能的,因为客户端不会将“锚点部分”发送到服务器

As an example, here's the exact request that Chrome generated after submitting http://example.com/#foobar (recorded using Wireshark): 例如,以下是Chrome在提交http://example.com/#foobar后生成的确切请求(使用Wireshark记录):

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

See, no #foobar. 看,没有#foobar。 So there's no way a server application can read it. 所以服务器应用程序无法读取它。

You could do some JavaScript magic to store the anchor in a cookie or in a hidden input field or whatever voodoo you're into. 你可以做一些JavaScript魔法来将锚存储在cookie或隐藏的输入字段或你所进入的任何伏都教中。 But it won't ever work for requests that don't originate from your own sites. 但它不适用于不是来自您自己网站的请求。 It's simpler to make whatever you need on the server part of the query string and use the anchor only for JavaScript-only tasks - or use it to navigate inside a simple HTML document - but that's so 90s ;). 在查询字符串的服务器部分上做任何你需要的东西更简单,并且仅将锚用于仅用于JavaScript的任务 - 或者使用它来在简单的HTML文档中导航 - 但这就是90年代;)。

Here's the important part from the mentioned RFC 1808: 这是提到的RFC 1808的重要部分:

Note that the fragment identifier (and the "#" that precedes it) is not considered part of the URL. 请注意,片段标识符(以及它之前的“#”)不被视为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. 但是,由于它通常在与URL相同的字符串上下文中使用,因此解析器必须能够在片段存在时识别该片段,并将其作为解析过程的一部分放在一边。

I guess you want to make a bookmarkable Ajax web-application and you want to replace certain fragments of the DOM without reloading the full page. 我想你想制作一个可收藏的Ajax网络应用程序,你想要替换DOM的某些片段而不重新加载整个页面。 (Otherwise you could use query parameters.) As sfussenegger mentions, browser doesn't transmit ' anchor ' to the server. (否则你可以使用查询参数。)正如sfussenegger所提到的,浏览器不会将“ ”传输到服务器。 The solution is on the client-side. 解决方案是在客户端。

Javascript window.location.hash gives the anchor information. Javascript window.location.hash给出了锚信息。 You can append an event handler to the window.onload event which grabs window.location.hash and transmits it to the server in an Ajax XHttpRequest. 您可以将事件处理程序附加到window.onload事件,该事件处理window.location.hash并将其传输到Ajax XHttpRequest中的服务器。 You catch the response and build it into the DOM. 您捕获响应并将其构建到DOM中。

Unfortunately, it's two client-server roundtrips. 不幸的是,这是两次客户端 - 服务器往返。

More on this at ajaxpatterns . 更多关于这个在ajaxpatterns

This solution would only work after a submit button has been pushed, but: you could use javascript to place a hidden value on your form that is set to the value of document.location. 此解决方案仅按下提交按钮后才起作用,但是:您可以使用javascript在表单上放置一个设置为document.location值的隐藏值。 That would tell you exactly what the browser is seeing as your address. 这会告诉您浏览器确切看到的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM