【问题标题】:read the query parameter from file path [duplicate]从文件路径中读取查询参数[重复]
【发布时间】:2016-05-13 07:12:06
【问题描述】:

我正在尝试读取 javascript 文件中的查询参数。

代码-

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Chat App List</title>
</head>
<body>
  <div id="app"></div>
  <script src="bundle.js?cid=vivek"></script>   // I am trying to read value of cid in bundle.js file
</body>
</html>

到目前为止,我尝试在捆绑文件中使用 window.location.href,但它给出了 undefined,因为它从 url 读取并且 url 服务于 index.html 文件

【问题讨论】:

  • 哪个查询参数?
  • 尝试获取script标签的引用并访问其src属性
  • 您尝试以这种方式通过查询字符串传递数据是否有原因?看来您的问题可能有更好的解决方案。
  • @sheeldotme bundle.js 并不完全是一个静态文件,它是一个与另一个节点服务器同步的 cdn 服务器路径,它将在读取 cid 后提供动态 js 文件,但那个 cid也需要在服务器返回的实际js内容中。

标签: javascript


【解决方案1】:

windows.location.href 将提供您在浏览器地址栏中看到的地址。

虽然您可以解析 DOM 并找出您需要的内容,但肯定没有推荐的方式在您的代码中获取该查询参数。

编辑:发现之前提出的相同问题 - Get the query string on the called javascript file

【讨论】:

  • window.localtion.href 将不起作用,因为文件是来自 index.html 的服务器。我从 DOM 中读过它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-15
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 2015-05-07
相关资源
最近更新 更多