Is there a simple way to get the current URL from an iframe?
If the two domains are mismatched, you'll run into cross site reference scripting security restrictions.
See also answers to a similar question.
If your iframe is from another domain, (cross domain), the other answers are not going to help you... you will simply need to use this:
var currentUrl = document.referrer;
and - here you've got the main url!
Hope this will help some how in your case, I suffered with the exact same problem, and just used localstorage to share the data between parent window and iframe. So in parent window you can:
And in code where iframe source is just get this data from localstorage:
Saved me a lot of time. As far as i can see the only condition is access to the parent page code. Hope this will help someone.
If you are in the iframe context,
you could do
const currentIframeHref = new URL(document.location.href); const urlOrigin = currentIframeHref.origin; const urlFilePath = decodeURIComponent(currentIframeHref.pathname);
If you are in the parent window/frame, then you can use https://stackoverflow.com/a/938195/2305243 's answer, which is
©2020 All rights reserved.