Getting iFrame’s Body content on IE and Firefox

A few days back i was working on a  problem for which i had to read contents from a body tag of an iframe. I tried the normal code which any developer will use :

var bodyContent=window.frames['frameName'].document.body.innerHTML;

To my surprise it was not cross browser compatible.

It didn’t work on both IE and Firefox.

After googling i figured it out how to make it work for both IE and Firefox. Here is the code

var iFrameId =  document.getElementById('frameId');
   //For Firefox
   if ( iFrameId .contentDocument )
   { 
     alert( iFrameId .contentDocument.getElementsByTagName('body')[0]);
   }
   else if ( iFrameId .contentWindow )
   { // internet explorer
     alert(iFrameId .contentWindow.document.getElementsByTagName('body')[0]);
   }

Have fun !!

This entry was posted in WEB 2.0, javascript and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*