473,390 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Get contents of HTML page in iframe, into Javascript variable

I have a page with an iframe, and a Javascript function that I need to get the contents of the iframe - what I mean by that is, the text from the list.html file that's in the iframe. It's actually a list of numbers (no formatting) but I'm happy to take it as a text string and go from there... it could be a .txt file but I heard at one point that .html would work better. Trouble is, I can't work out how to access the contents of the iframe from the script. Here's what I have. I'm trying to keep it as simple as possible so I can build around it when I get it working.
Expand|Select|Wrap|Line Numbers
  1. <html>
  3. <head>
  4. <script language="JavaScript">
  5. function getList() {
  6.     var list = document.getElementById('myFrame').innerHtml;
  7.     alert(list);
  8. }
  9. </script>
  10. </head>
  12. <body>
  13. <iframe name="myFrame" id="myFrame" src="list.html"><iframe>
  14. <a href="javascript:getList()">Get List</a>
  15. </body>
  17. </html>
I'm really clueless here - it's the first time I've tried to do any input in Javascript other than forms. I have done extensive googling and experimented by myself, but everything I try gets me either no alert at all, or an alert that just reads "undefined". I've tried 'getElementById', 'window.frames("myFrame")', every combination of document.body and window.document.body imaginable... is there something obvious I'm missing?
Jul 26 '07 #1
1 11803
Parent HTML
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  3. <html>
  4. <head>
  5.     <title>Untitled</title>
  6. </head>
  8. <style>
  9. iframe {height:200px; width:300px; border:1px solid #000}
  10. </style>
  12. <script>
  13. function iframeContent() {
  14. frame = document.getElementById("myFrame")
  16.     if (frame.contentDocument) {
  17.     alert(frame.contentDocument.getElementById("test").innerHTML)
  18.     }
  19.     else if (frame.contentWindow) {
  20.     alert(frame.contentWindow.document.getElementById("test").innerHTML)
  21.     }
  22. }
  23. </script>
  25. <body>
  27. <iframe src="document.html" id="myFrame">
  28. </iframe><br>
  30. <button onclick="iframeContent()">OK</button>
  32. </body>
  33. </html>
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  3. <html>
  4. <head>
  5.     <title>Untitled</title>
  6. </head>
  8. <body>
  10. <p id="test">Text</p>
  12. </body>
  13. </html>
Just an example showing how you can acess elements within an iframe!
Good luck!

ps: working on ie7 now
Jul 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

by: John Chen | last post by:
I need to use iframe to create a floating frame. But the contents in the iframe is not a external html file. Rather, it will be dynamically created by jsp. How can I set the src attribute to a URL...
by: TLM | last post by:
I am trying to build a web application that will contain links to files on a users local computer. I am assuming that the files will be in a known location and can display in a browser window. ...
by: syshex | last post by:
Hi everyone ! I'm really new to javascript, and am possibly aiming quite high already, but still I feel I just satisfy my curiosity and ask you all this question. I've got this really really...
by: Mike | last post by:
I have an intranet page which has an iframe which links to a number of different possible .NET pages. In one of the embedded .NET pages (and soon to be others) we want to be able to capture...
by: jaktharkhan | last post by:
Hi, I really really need help in trying to figure out how can I do a CloneNode on an Iframe where the cloned IFRAME clones with all its contents?. Basically what I am doing is dynamically building...
by: Peter Gustafsson | last post by:
Hi I have a HTML-only file, and asp file that do a databasequery. How can I do a query in the html file, like this: DBLookup("2"). The asp file execute the query and respond with the result....
by: bs9999 | last post by:
I'm using IFRAME to connect back to my ASP.NET page in order to fetch the FreeBox HTML Editor html contents and scripts (i do this apprach because the html editor is fairly heafty to send to the...
by: volker | last post by:
Hi, I'm trying to move an iframe from one position in the DOM to another. The moving itself is not a problem at all, but I experience a different behaviour between Firefox (1.5) and IE (6): As...
by: mevryck | last post by:
Greetings I have a huge Javascript with inclusion of external scripts and all. I got this by doing a XSLT . Now I have the contents in a Javascript variable, but I'm not able to update the...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.