Posts

Showing posts with the label Cross Domain

Access Parent URL From Iframe

Answer : Yes, accessing parent page's URL is not allowed if the iframe and the main page are not in the same (sub)domain. However, if you just need the URL of the main page (i.e. the browser URL), you can try this: var url = (window.location != window.parent.location) ? document.referrer : document.location.href; Note: window.parent.location is allowed; it avoids the security error in the OP, which is caused by accessing the href property: window.parent.location.href causes "Blocked a frame with origin..." document.referrer refers to "the URI of the page that linked to this page." This may not return the containing document if some other source is what determined the iframe location, for example: Container iframe @ Domain 1 Sends child iframe to Domain 2 But in the child iframe... Domain 2 redirects to Domain 3 (i.e. for authentication, maybe SAML), and then Domain 3 directs back to Domain 2 (i.e. via form submissi...

Access Parent Window From Iframe (cross-domain)

Answer : If I were you I would check out window.postMessage. It may do what you want: For reference see the following: MDN - Window.postMessage https://stackoverflow.com/a/3076648/296889 - see the Window.postMessage section If I understand correctly, all modern browsers do now allow to do this. So I'm here to find the best solution. This is your solution. What you're asking is not possible. See related questions: How to access parent Iframe from JavaScript <iframe> javascript access parent DOM across domains? Cross-domain access in iframe from child to parent EDIT As mentioned in the comments below, @JeremysAwesome's answer offers a method that would allow cross-domain requests under certain circumstances. See the SO question below for more information. Ways to circumvent the same-origin policy but you can change the src attribute of the iframe (adding a #hashtag for example) and listen to the onhashchange event in the child window...