Accessing Parent URL from iframe: Limitations and Alternative Solutions
Accessing the URL of the parent page from an iframe poses limitations when the iframe and the main page reside on different subdomains. Despite being on the same domain, subdomains are treated as distinct entities for security reasons, preventing cross-frame access.
Accessing Parent URL from Same Subdomain
If both the parent page and iframe reside on the same subdomain, one can use the following approach to retrieve the parent page's URL:
parent.window.location.href
Using Alternative Methods
However, when dealing with different subdomains, a viable alternative method is available for situations where only the main page's URL is necessary:
var url = (window.location != window.parent.location) ? document.referrer : document.location.href;
Explanation
The above is the detailed content of How Can I Access the Parent URL from an iframe When It's on a Different Subdomain?. For more information, please follow other related articles on the PHP Chinese website!