Here's how to get a reference to an iframe with JavaScript, in Firefox
var iframeEl = document.getElementById("iframeId");
var iframeDoc = iframeEl.contentDocument
var iframeWin;
if (iframeDoc) { // Firefox
iframeWin = iframeDoc.defaultView
} else { // IE
iframeWin = iframeEl.contentWindow
}
if (iframeEl.contentWindow) { // IE and Safari require, but works for Firefox too
iframeWin = iframeEl.contentWindow;
} else if (iframeEl.contentDocument) { // Works for Firefox, DOM level 2 standard
iframeWin = iframeEl.contentDocument.defaultView;
}
And then you can use iframeWin to get access to JavaScript objects and the DOM of the iframe.
Also, I learned that getElementById() in IE (version 7 tested) picks up elements by name attribute as well, so names and ids need to be unique for all elements on a page for IE.
2 comments:
This saved my bacon this week. Thanks a ton!
Thanks so much for posting this. I have spent many hours trying to figure this out. This technique works great on IE, FF and Chrome as of April 2011.
Post a Comment