Sendoh
Sendoh

Reputation: 25

javascript (greasemonkey) how to access a newly created iframe

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

Upvotes: 0

Views: 2589

Answers (1)

Tim Down
Tim Down

Reputation: 324567

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;
}

Upvotes: 1

Related Questions