简体   繁体   English

当“#”在 url 中时,URLSearchParams 无法获取第一个参数

[英]URLSearchParams fails to get first parameter when '#' is in url

This seems like a bug in this API, though I don't know where to report it.这似乎是这个 API 中的一个错误,虽然我不知道在哪里报告它。 So I am posting here for help.所以我在这里发帖寻求帮助。 Mozilla docs here URLSearchParams Mozilla 文档在这里URLSearchParams

If window.location.href contains a # then URLSearchParams.get fails to retrieve the first search parameter如果window.location.href包含#URLSearchParams.get无法检索第一个搜索参数

const location = 'http://localhost:3000/path?referrer=https://google.com';
const myURL = new URL(location).searchParams.get('referrer')
// myURL === 'https://google.com

// in one line:
(new URL('http://localhost:3000/path?referrer=https://google.com')).searchParams.get('referrer')

But this same example slightly tweaking the window location will fail但是这个稍微调整窗口位置的例子会失败

const location = 'http://localhost:3000/#/path?referrer=https://google.com';
const myURL = new URL(location).searchParams.get('referrer')
// myURL === null

// in one line
(new URL('http://localhost:3000/#/path?referrer=https://google.com')).searchParams.get('referrer')

This example is using new URL(location).searchParams.get but will yield the exact same functionality if you tweak it to using new URLSearchParams(...).get此示例使用new URL(location).searchParams.get但如果您将其调整为使用new URLSearchParams(...).get将产生完全相同的功能

In URL standards, # introduces a fragment component that has no special meaning to the transport protocol and is treated as secondary or "user specified" information whose semantics are treated as unknown.在 URL 标准中, #引入了一个片段组件,它对传输协议没有特殊意义,被视为次要或“用户指定”的信息,其语义被视为未知。 The # and everything following it in the URL is passed to the client application ( eg HTML browser) without interpretation. URL 中的#和它后面的所有内容都被传递到客户端应用程序(例如 HTML 浏览器)而无需解释。 You can access the fragment value in JavaScript using window.location.hash .您可以使用window.location.hash在 JavaScript 中访问片段值。

Any URL parameters must precede a fragment identifier or they will be included in fragment content and not parsed as parameters.任何 URL 参数都必须位于片段标识符之前,否则它们将包含在片段内容中而不被解析为参数。

A # could be included in the URL path or parameters, but would need to be percentage escaped as %23 . #可以包含在 URL 路径或参数中,但需要将百分比转义为%23 I would strongly advise against writing a router or creating a server side folder for static content that contains # in the path, even if technically possible.我强烈建议不要为路径中包含#静态内容编写路由器或创建服务器端文件夹,即使技术上可行。

Allowing users to enter # in form inputs would normally be handled automatically by encoding the input value with encodeURIComponent() before submitting the form.允许用户在表单输入中输入#通常会通过在提交表单之前使用encodeURIComponent()对输入值进行编码来自动处理。

Data URLs that have been formulated using clear text of a particular MIME type need to percentage escape any and every # within the text to avoid truncating data represented by the URL.使用特定 MIME 类型的明文制定的数据 URL 需要对文本中的所有#进行百分比转义,以避免截断 URL 表示的数据。

I know this issue is a bit old, but I ran into the same issue recently and created a simple function to fix the Url hash & search params:我知道这个问题有点老了,但我最近遇到了同样的问题,并创建了一个简单的函数来修复 Url 哈希和搜索参数:

   function fixUrlHash(url) {
  let fixedUrl = new URL(url);
  let search = url.search;
  let hash = url.hash;
  const position = url.hash.indexOf('?');
  if(search.length <= 1 && position >= 0) {
    search = hash.substr(position);
    hash = hash.substr(0, position);
    fixedUrl.hash = hash;
    fixedUrl.search = search;
    fixedUrl.href = fixedUrl.toString();
  }
  return fixedUrl;
}

This simply returns a new URL object with the fixed hash & search params.这只是返回一个带有固定哈希和搜索参数的新 URL 对象。 After you get the fixed URL value, you can load the new URL in the browser if original URL is the browser location.获取固定的 URL 值后,如果原始 URL 是浏览器位置,则可以在浏览器中加载新的 URL。

Just replace # with /只需将#替换为/

let location = 'http://localhost:3000/#/path?referrer=https://google.com';
location = location.replace('#', '/');
let myURL = new URL(location).searchParams.get('referrer')
console.log(myURL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM