URL hash format, what's allowed and what's not?

爷,独闯天下 提交于 2020-01-04 14:13:25

问题


I'm using hash-based navigation in my rich web app. I also found I needed to create permalinks that would point to single instances of resources, but since I cannot cause the page o refresh, and the main page is loaded from a single path '/', I cannot use real URLs. Instead I thought about using hashes. Let me give you an example because I know the explanation above sucks.

So, instead of having http://example.com/path/to/resource/1, I would have http://example.com/#path/to/resource/1

This seems to work ok, and browser believes '#path/to/resource/1' is a hash (slashes permitted, I think) but I was wondering about what characters are allowed in URL hash. Is there a specification or a RFC that I could read to find out what the standard behavior of browsers is when it comes to hashes?

EDIT: Ok, so silly me. Didn't actually check if slashes worked in all browsers. Chrome obviously doesn't like them. Only works in FF.


回答1:


Look at: http://www.w3.org/Addressing/rfc1630.txt or http://www.w3.org/Addressing/URL/4_2_Fragments.html

Basically you can use anything that can be encoded in an URL.

Note: There might be browser inconsistencies. If you fear them, you might use a serialization mechanism, like converting the string to hex or something (will be twice longer though), or use an id of some sort.




回答2:


This document should help. Slashes are allowed, but the lexical analysis might differ between browsers.




回答3:


I think you might find that useful: RFC3986

If you use PHP to generate your page paths you could also urlencode() which generates you a valide URL.



来源:https://stackoverflow.com/questions/5867431/url-hash-format-whats-allowed-and-whats-not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!