HTML 编码的链接和锚点
我有一个用例,我将页面焦点设置为特定元素(前面有一个锚点)。 当用户未登录时,系统会重定向到登录页面,登录后,用户会重定向到相关页面,并对 URL 进行编码。
我发现 link#target 形式的 URL 按预期工作(重点关注元素),而 url 编码的链接 link%23target 则不然。 这是预期的行为吗?
编辑:如果这是预期的行为,是否有解决方法可以专注于目标? 比如,绕过 url 编码的方法?
编辑添加更多信息:
假设有一个代码
page1.html
... html before the anchor ...
<a name="test">Some code</a>
... html after the anchor ...
我正在访问 page1.html%23test 页面。 这与 page1.html#test 的工作方式不同。 有没有 jQuery 方法来实现这个? 即使对 location.hash 进行 url 编码后,它还会包含 test 吗? 我无法控制更改 url 编码。
编辑:
因为我知道页面重定向后我想要转到哪个命名锚点,所以我做了一个
window.location.hash = namedAnchor
来解决这个问题。 仅当客户成功登录时才会输出此 JS 行。解决了我的问题,但不是我正在寻找的通用答案。 我正在寻找一种方法来避免 url 编码中 # 的转义。
I have a use case where I am setting the page focus to a particular element (having an anchor before it). When a user is not signed in, there is a redirect to the login page and after signing in, the user is redirected to the page in question, with the URL encoded.
I see that a URL of the form link#target works as expected (focusing on the element) while the url encoded link link%23target doesn't. Is this expected behavior?
Edit: If this is the expected behavior, is there a work around to focus on the target? As in, a way around url encode?
Edit adding more info:
Assuming that there is a code
page1.html
... html before the anchor ...
<a name="test">Some code</a>
... html after the anchor ...
I am accessing the page as page1.html%23test. This doesn't work the same way as page1.html#test. Is there a jQuery method to implement this? Would location.hash contain test even after it has been url encoded? I have no control on changing the url encoding.
Edit:
As I knew which named anchor I wanted to go to after page is redirected, I did a
window.location.hash = namedAnchor
to solve the issue. This JS line is output only if a customer is successfully signed in. Solved my issue, though not the generic answer I was looking for. I was looking for a way to avoid escaping of # in url encode.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。 将 # 编码为 %23 有效地表示“我只是指一个普通的旧“#”字符,而不是 URL 片段”。 其他保留字符也是如此:转义它们会阻止它们在 URL 中具有特殊含义。
在您的情况下,您确实希望在将 URL 作为参数传递到登录页面时对其进行编码,但您的登录页面应在执行重定向之前对 URL 进行解码。
Yes. Encoding the # as %23 effectively says "I just mean a plain old "#" character, not a URL fragment". The same is true of other reserved characters: escaping them stops them from having special meaning in the URL.
In your case you do want to encode the URL when passing it to your login page as a parameter, but your login page should decode the URL before performing the redirect.
您可以使用 PHP 或其他脚本语言解析该字符串,也可以使用encodeURIComponent 使用 JavaScript 解析该字符串。 我为此写了一篇文章,您可以查看 http://www.stoimen.com/blog/2009/05/25/javascript-encode-cyrillic-symbols-with-encodeuricomponent/
希望可以帮助您。 然而,尽管有默认行为,您仍必须使用任一方法进行检查。
You can both parse this string with PHP or other script language, or with JavaScript using the encodeURIComponent. I wrote an article for that, you can check on http://www.stoimen.com/blog/2009/05/25/javascript-encode-cyrillic-symbols-with-encodeuricomponent/
Hope that can help you. However despite the default behavior you must check with either method.