【问题标题】:How to retrieve atributes from iframe?如何从 iframe 中检索属性?
【发布时间】:2021-10-05 03:02:36
【问题描述】:

我有一个应用程序,我正在使用 iframe 插入另一个应用程序,如下所示

父应用

<HTML>
....
<body>
<iframe firstname="Trump" src="https://on.co/onboarding/" height="600" allowfullscreen="" > 
</iframe>

</body>
</html>

儿童应用

<HTML>
....
<body>
  <h1> Covid is deadly if you doubt  ask the indians </h1>

  <script>
   const urlParams = new URLSearchParams(document.location.search);
   const myParam = urlParams.get('firstname');
   console.log(myParam) //undefined
  </script>
</body>
</html>

我想从 iframe URL 参数中获取名字

我没有定义

这里有什么问题?

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/a/28296334/13983399
  • 网址没有搜索参数
  • @Musa 你是什么意思?
  • https://on.co/onboarding/ 没有名字参数,它根本没有参数
  • 好的,我更改了如何从 iframe 获取属性

标签: javascript html jquery dom iframe


【解决方案1】:

我想你只需要:

<iframe src="https://on.co/onboarding/?firstname=SomeName" height="600" allowfullscreen=""></iframe>

【讨论】:

    【解决方案2】:

    听起来您正在尝试获取元素 attribute,而不是 url parameter。您可以在元素上使用getAttribute 方法获取属性,例如

    const myIframe = document.querySelector('iframe');
    const firstname = myIframe?.getAttribute('firstname');
    

    https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute

    但是请注意,firstname 属性在 iframe 上是无用的,因此您应该重新考虑一开始是如何获得它的。

    【讨论】:

    • @TheDeadMan 我看到你的问题,但它与你提供的 sn-p 不一致。您显示的 URL 根本没有 firstname URL 参数,但是 iframe 确实 具有 firstname 属性。所以你的问题不是“如何获取参数”,而是“我的 iframe 一开始是怎么变成这样的”
    • iframe 在父框架中,所以你不能搜索document (你不能跨域访问它)
    • 谢谢@Quentin,我看了一眼。然而,IMO 的问题仍然是 iframe 对于 OP 的预期无效
    • 我换个问题也许现在无法理解
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 2011-08-03
    相关资源
    最近更新 更多