【问题标题】:Putting a fragment identifier inside of a query parameter将片段标识符放在查询参数中
【发布时间】:2016-10-24 21:53:57
【问题描述】:

我正在尝试重定向到包含名为 next 的查询参数的 URL 路径,该参数本身就是包含片段标识符的 URL 路径。这是我尝试过的 JavaScript 代码:

window.location.href = "/gauth?next=" + window.location.pathname + "#onload=exportToDrive"

为了清楚起见,#onload=exportToDrive 应该属于next URL 路径(不是浏览器重定向到的 URL)。如何将这些信息指定给浏览器,以便它能够正确处理这种情况?

【问题讨论】:

  • 一般情况下,next 参数的整个值应该使用类似encodeURIComponent() 的 URL 编码。
  • @robertklep 工作就像一个魅力!非常感谢!如果您想对答案发表评论,我很乐意给您一个赞成票和接受的答案:)。

标签: javascript http url query-parameters fragment-identifier


【解决方案1】:

您应该始终正确编码 URL 参数值,使用类似 encodeURIComponent 的函数:

window.location.href = "/gauth?next=" + encodeURIComponent(window.location.pathname + "#onload=exportToDrive");

这将确保任何片段标识符(以及查询字符串参数)都不会应用于/gauth

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    相关资源
    最近更新 更多