Get the content of an IFRAME in Javascript-crossbrowser solution for both IE and Firefox

Source: Internet
Author: User

Get the content of an IFRAME in Javascript-crossbrowser solution for both IE and Firefox

Http://roneiv.wordpress.com/2008/01/18/get-the-content-of-an-iframe-in-javascript-crossbrowser-solution-for-both-ie-and-firefox/

 

 

OK, let's imagine the use case: I have an IFRAME somewhere on my page, and when I click a link or a button I need to get the content of it (cocould be a textarea e.g .), and then do some stuff with it.

It was easy to do this in IE, but for Firefox I struggled more, as I kept getting the "frame has no properties" error message in the console. and when I solved this I couldn't get to the content.

There is a lot of references out there claiming that you cocould use document. frames ['nameofmyiframe'] or window. frames ['nameofmyiframe'] To get the frame, and then use. innerhtml to get the content, but both are wrong.

I came up with the following function that seems to do the job in both Firefox (tested on version 2.0.0.11 and 3.03) and in IE (6 and 7 ):

 

 

Function getcontentfromiframe (iframename)
{

VaR myiframe = Document. getelementbyid (iframename );
VaR content = myiframe.ContentWindow. Document. Body. innerhtml;

// Do whatever you need with the content

}

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.