Getting a reference to the parent IFRAME
A document is not directly connected to its parent document. You do need a reference to window
in order to pick up the parent
.
The DOM Level 2 Views property document.defaultView
will give you the window
in most modern web browsers, but in IE you have to use the non-standard document.parentWindow
instead. (Some older or more obscure browsers don't implement either of these properties, in which case you're stuck.)
That'll give you the window
of the parent document. If you want to get the <iframe>
that holds your document, you'll have to iterate through all the iframes on the page and check whether the contained document is yourself.
Again, the method to get from an iframe element back to the child is gratuitously different in IE (iframe.contentWindow
giving you the window
) vs the DOM standard and everyone else (iframe.contentDocument
giving you the document
).
So, something like:
function getFrameForDocument(document) {
var w= document.defaultView || document.parentWindow;
var frames= w.parent.document.getElementsByTagName('iframe');
for (var i= frames.length; i-->0;) {
var frame= frames[i];
try {
var d= frame.contentDocument || frame.contentWindow.document;
if (d===document)
return frame;
} catch(e) {}
}
}
(The try...
is to avoid crashing the loop out when a document access fails due to another iframe being on a different domain, causing a Same Origin Policy error.)
Access parent URL from iframe
You're correct. Subdomains are still considered separate domains when using iframes. It's possible to pass messages using postMessage(...)
, but other JS APIs are intentionally made inaccessible.
It's also still possible to get the URL depending on the context. See other answers for more details.
Access elements of parent window from iframe
I think the problem may be that you are not finding your element because of the "#" in your call to get it:
window.parent.document.getElementById('#target');
You only need the # if you are using jquery. Here it should be:
window.parent.document.getElementById('target');
How to get the parent iframe's parent iframe using JavaScript?
Use window.parent.parent
to get the grandparent window. So you want:
window.parent.parent.document.body.style.backgroundColor = "red";
And if you want the top-most parent, no matter how many levels deep you are, you can use window.top
. See
Find most upper parent window with javascript
How to get reference of iFrame from its caller in parent window
If the msgDiv is in the iframe, you need to pass window too. For example LX.print_msg(window,"msgDiv");
and use
this.print_msg(win,divID){
win.document.getElementById(divID).innerHTML = "some computed message";
Related Topics
CSS Overflow Hidden Increases Height of Container
How to Include CSS Files from an MVC Partial Control
Adding Custom CSS and Js to Shopify
JavaScript -Change CSS Color for 5 Seconds
How to Achieve a Consistent Layout in All Browsers
Make Overlapping Div "Not Clickable" So That Content Below Can Be Accessed
Check Visibility of an Object with JavaScript
Html5 Video Custom Additional Seek Bar
Single Navigation Bar Across Website
Scrolling Log File (Tail -F) Animation Using JavaScript
Jquery - "Fadein Is Not a Function"
Change CSS Link Property Onclick with JavaScript/Jquery
Internet Explorer Adds Height- and Width-Attributes to a Newly Appended Image Automatically
Change Header CSS Upon Scrolling Down
How to Enable the JavaScript Error/Debug Console for Safari Within Android