Retrieve URL of IFrame On Parent Page After IFrame URL Changes

I need an example of code that will add the URL of an IFrame to a text field on the parent page upon navigation from a 1st to a 2nd page in the IFrame.

All code must be added to the parent page, HTML Element, or the 2nd page that is navigated to in the IFrame. Code cannot be added to the 1st page shown in the IFrame. It is also important that the script runs automatically based on something like when the 2nd page is loaded or when the URL of the HTML Element is changed (not something like a button click).

Is this possible?

One challenge is that all of the examples include a button click when postMessage & onMessage are used. I have been able to postMessage automatically from the parent page to the HTML Element by placing the code in the onReady event handler of the parent page but I am unable to figure it out the other direction.

In advance, help is much appreciated.

I ultimately need an example of code but can anyone at least tell me if this is possible? I am having a hard time finding anyone who can help. Thanks.

It should be possible by creating an iframe inside your HTML Component. You could create a function that uses window.onhashchange and send a message to the parent with window.parent.postMessage(‘your message’, ‘*’);