HTML 编码的链接和锚点

发布于 2024-07-27 05:02:14 字数 770 浏览 6 评论 0原文

我有一个用例,我将页面焦点设置为特定元素(前面有一个锚点)。 当用户未登录时,系统会重定向到登录页面,登录后,用户会重定向到相关页面,并对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说谎友 2024-08-03 05:02:14

是的。 将 # 编码为 %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.

白云悠悠 2024-08-03 05:02:14

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文