When looking out for ways to deal with #document under iframe, it is essential to consider iframe as a build that is enclosed within another document or data file into an HTML to display the embedded data or code in a sub-window within the current window of the browser. It is, therefore, impossible to treat one document as independent of another.
The Ultimate Way Out
An inline frame element is usually found in this form:
<iframe src="URL" more attributes> alternative content for browsers which do not support iframe </iframe> The documents which are referred to by the URL in a particular subwindow
The documents which are made according to the URL in a sub-window are displayed with either vertical/horizontal scroll bars in browsers that support inline frame. These types of browsers display regardless of the contents of the inline frame element, while browsers that don’t support the inline frame take the opposite action by processing the contents of the iframe element with the assumption that the <iframe…> and </iframe> tags were absent. This leads to the conclusion that the contents of the iframe elements are essential regardless of the negligence by a few browsers.
// reference to iframe with id 'ifrm' var ifrm = document.getElementById('ifrm'); ifrm.style.width = '400px'; // set width ifrm.src = 'newpage.html'; // set src to new url
// using reference to iframe (ifrm) obtained above var win = ifrm.contentWindow; // reference to iframe's window // reference to document in iframe var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document; // reference to form named 'demoForm' in iframe var form = doc.getElementById('demoForm');
Immediately the references to the window and document have been acquired; it is possible to gain access to any object or property bound within the iframe document.