从 URI 获取锚点
我正在编写一个 JSP/Servlet,并且尝试获取 URI 的锚点部分,例如:
blabla.rdf#mark
如何从请求中获取标记?显然 request.getParameter()
不起作用?
欢迎任何帮助。
I'm writing a JSP/Servlet and I'm trying to get the the anchor part of the URI e.g:
blabla.rdf#mark
How do I get my mark from my request? Obviously request.getParameter()
doesn't work?
Any help would be welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是不可能的,因为客户端不会将“锚点部分”发送到服务器
作为示例,以下是 Chrome 在提交
http://example.com/#foobar
后生成的确切请求(使用 Wireshark 录制):看,没有#foobar。所以服务器应用程序无法读取它。
您可以执行一些 JavaScript 魔法,将锚点存储在 cookie 中、隐藏的输入字段中或任何您喜欢的巫术中。但它永远不会适用于并非源自您自己网站的请求。更简单的做法是在服务器上将您需要的内容作为查询字符串的一部分,并仅将锚点用于仅 JavaScript 任务 - 或者使用它在简单的 HTML 文档中导航 - 但这已经是 90 年代的事了;)。
以下是提到的 RFC 1808 中的重要部分:
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):See, no #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. 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 ;).
Here's the important part from the mentioned RFC 1808:
我想您想要制作一个可添加书签的 Ajax Web 应用程序,并且想要替换 DOM 的某些片段而不重新加载整个页面。 (否则您可以使用查询参数。)正如 sfussenegger 提到的,浏览器不会将“anchor”传输到服务器。解决方案是在客户端。
Javascript
window.location.hash
提供锚点信息。您可以将事件处理程序附加到window.onload
事件,该事件获取window.location.hash
并将其通过 Ajax XHttpRequest 传输到服务器。您捕获响应并将其构建到 DOM 中。不幸的是,这是两次客户端-服务器往返。
有关此内容的更多信息,请访问 ajaxpatterns。
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. (Otherwise you could use query parameters.) As sfussenegger mentions, browser doesn't transmit 'anchor' to the server. The solution is on the client-side.
Javascript
window.location.hash
gives the anchor information. You can append an event handler to thewindow.onload
event which grabswindow.location.hash
and transmits it to the server in an Ajax XHttpRequest. You catch the response and build it into the DOM.Unfortunately, it's two client-server roundtrips.
More on this at ajaxpatterns.
此解决方案仅在按下提交按钮后才有效,但是:您可以使用 JavaScript 在表单上放置一个隐藏值,该值设置为 document.location 的值。这将准确地告诉您浏览器将您的地址视为什么。
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. That would tell you exactly what the browser is seeing as your address.