URL锚点中有哪两个分隔符可以使用? [英] What two separator characters would work in a URL anchor?

查看:646
本文介绍了URL锚点中有哪两个分隔符可以使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网址中使用锚点,允许用户在网络应用程序中为活动网页添加书签。我使用锚点因为它们很容易适应GWT历史机制。

I use anchors in my URLs, allowing people to bookmark 'active pages' in a web application. I used anchors because they fit easily within the GWT history mechanism.

我现有的实现将导航和数据信息编码到锚中,用' - '字符分隔。即创建像#location-location-key-value-key-value这样的锚

My existing implementation encodes navigation and data information into the anchor, separated by the '-' character. I.e. creating anchors like #location-location-key-value-key-value

除了负值(如-1)导致严重的解析问题之外,它可以正常工作,但现在我发现有两个分隔符会更好。另外,给出负数问题,我想放弃使用' - '。

Other than the fact that negative values (like -1) cause serious parsing problems, it works, but now I've found that having two separator characters would be better. Also, givin the negative number issue, I'd like to ditch using '-'.

URL锚点中哪些其他字符不会干扰URL还是它的GET参数?这些将来会有多稳定?

What other characters work in a URL anchor that won't interfere with the URL or its GET params? How stable will these be in the future?

推荐答案

查看 RFC的URL,第3.5节片段标识符(我相信你指的是)定义为

Looking at the RFC for URLs, section 3.5 a fragment identifier (which I believe you're referring to) is defined as


fragment    = *( pchar / "/" / "?" )

附录A


pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
                 / "*" / "+" / "," / ";" / "="

有趣的是,规范还说

允许使用字符斜杠(/)和问号(?)来表示片段标识符中的数据。

"The characters slash ("/") and question mark ("?") are allowed to represent data within the fragment identifier."

所以它看起来像真正的锚点,如

So it appears that real anchors, like

<a href="#name?a=1&b=2">
....
<a name="name?a=1&b=2">

应该是合法的,与普通的URL查询字符串非常相似。 (快速检查验证这些在至少chrome,firefox和ie中正常工作)因为这有效,我假设你可以使用你的方法来获得像

are supposed to be legal, and is very much like the normal URL query string. (A quick check verified that these do work correctly in at least chrome, firefox and ie) Since this works, I'm assuming you can use your method to have URLs like

http://www.site.com/foo.html?real= 1& parameters = 2 #fake = 2& parameters = 3

没有问题(例如'参数'变量in该片段不应该干扰查询字符串中的那个)

with no problem (e.g. the 'parameters' variable in the fragment shouldn't interfere with the one in the query string)

您还可以在必要时使用百分比编码...并且在子语言中定义了许多其他字符可以使用的delims。

You can also use percent encoding when necessary... and there are many other characters defined in sub-delims that could be usable.

注意:

另外来自规范:

片段标识符组件由数字符号(#)字符的存在表示,并以URI的末尾结束。

"A fragment identifier component is indicated by the presence of a number sign ("#") character and terminated by the end of the URI."

所以#之后的所有内容都是片段标识符,不应该干扰GET参数。

So everything after the # is the fragment identifier, and should not interfere with GET parameters.

这篇关于URL锚点中有哪两个分隔符可以使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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