Ancora Imparo - I'm still learning (Michelangelo at 87)

Ways to deal with #document under iframe

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.

Using JavaScript to Communicate iframe

JavaScript can be utilized in communicating with the iframe and the documents within it. The first of the few steps is to acquire the references to the iframe, the properties, and finally, the contents within it. To acquire the references to an iframe, you should use the getElementById. The references acquired can then be used to edit the properties found on the iframe.

// 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

Other properties entailed in the iframe element include both the contentWindow and contentDocument properties. References to the iframe documents window and objects can be acquired from these properties using JavaScript. This step is analyzed below:

// 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.

About the Autor

Jamiu Idowu

Jamiu Idowu

Entrepreneur seeking to shape the world through IT and emerging technologies. Enjoys research and technical writing, and can serve as a bridge between technology and its users.

Liked this article?

We have a lot more where that came from!
Join the subscribers who stay ahead of the pack.

By entering your email, you agree to our Terms of Service and Privacy Policy.