views:

147

answers:

1

hi there,

i have this website, by clicking on certain link on the website an iframe will be created within the same window.

my question, just wondering how to access this iframe? i tried document.getElementById() return null tried window.frames not there

Thanks

+1  A: 

If you've given the <iframe> element an ID then document.getElementById() will work. If you're trying to get hold of its window object then you'll need to use the iframe element's contentWindow property (or use the more standard contentDocument, which is a reference to the iframe's document object but is sadly absent from IE, at least up to and including version 7):

var iframe = document.getElementById("your_iframe_id");
var iframeWin, iframeDoc;
if (iframe.contentDocument) {
    iframeDoc = iframe.contentDocument;
    iframeWin = iframeDoc.defaultView;
} else if (iframe.contentWindow) {
    iframeWin = iframe.contentWindow;
    iframeDoc = iframeWin.document;
}
Tim Down
it gives me nullthis is the iframe html<iframe scrolling="no" frameborder="0" allowtransparency="true" style="width: 739px; height: 579px;" onload="showIframe()" name="iframeContentabc" id="iframeContent" src="" hspace="0"></iframe>,i use document.getElementById('iframeContent'); it returns me null
Sendoh
just found out the code running on one of the iframe loldocument.getElementByID workswindow.parent.document.getElementByIdbtw, with chrome window.parent will return undefined is there a workaround ?Thx
Sendoh