Can I get the current url of an iframe if CORS between my website and the third party site is enabled? If so, how?

20 Views Asked by At

I'm working on a website that acts as a hub for multiple website and has a navigation bar on the side with links to each that will get rendered in an iframe when clicked. I'm trying to make it so you can leave one of those tabs and then when you go back, it will pick up from where you left off.

When I try to get the current url using iframe.contentWindow.location.href, I obviously get same-origin policy errors. I know you can use window.postMessage() to at least allow communication between the window and iframe but that still doesn't make iframe.contentWindow.location.href work. If I enable CORS with my website inside of the 3rd party sites with http headers, how should I get the current url of the iframe?

The servers of the 3rd party sites are linux servers and my website is made with React + Vite. Not looking for a full solution to my code, just pointers to help me get going in the right direction.

0

There are 0 best solutions below