-1

I have an app which I am inserting another app using iframe as follows

Parent App

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

</body>
</html>

Children App

<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>

I would like to get the first name from the iframe URL parameters

I get undefined

What is wrong here?

The Dead Man
  • 6,258
  • 28
  • 111
  • 193

2 Answers2

0

I think you just need:

<iframe src="https://on.co/onboarding/?firstname=SomeName" height="600" allowfullscreen=""></iframe>
MBiabanpour
  • 370
  • 1
  • 13
-2

It sounds like you're trying to get an element attribute, not a url parameter. You can get attributes using the getAttribute method on an element, e.g.

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

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

Note however that the firstname attribute is useless on an iframe, so you should reconsider how you got it there in the first place.

Josh Fraser
  • 196
  • 1
  • 1
  • 8
  • @TheDeadMan I see your question, but it's not consistent with the snippet you've provided. The URL you showed simply does not have a `firstname` URL parameter, however the iframe *does* have a `firstname` attribute. So your problem is less 'how to get the parameter', and instead 'how did my iframe get like this in the first place' – Josh Fraser Jul 29 '21 at 13:18
  • 3
    The iframe is in the parent frame, so you can't search `document` for it (and you can't access it across origins) – Quentin Jul 29 '21 at 13:19
  • Thanks @Quentin, I glanced over that. However IMO the problem is still that the iframe is invalid for what OP expects – Josh Fraser Jul 29 '21 at 13:22
  • I change the question maybe now is undestandable – The Dead Man Jul 29 '21 at 13:32