我有一个应用程序,我正在使用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参数中获得名字
我得到undefined
怎么了?
我想你只需要:
<iframe src="https://on.co/onboarding/?firstname=SomeName" height="600" allowfullscreen=""></iframe>
听起来您正在尝试获取元素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
上是无用的,因此您应该重新考虑最初是如何获得它的。