473,405 Members | 2,176 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,405 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>
  2.  
  3. <head>
  4. <script language="JavaScript">
  5. function getList() {
  6.     var list = document.getElementById('myFrame').innerHtml;
  7.     alert(list);
  8. }
  9. </script>
  10. </head>
  11.  
  12. <body>
  13. <iframe name="myFrame" id="myFrame" src="list.html"><iframe>
  14. <a href="javascript:getList()">Get List</a>
  15. </body>
  16.  
  17. </html>
  18.  
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 11809
Parent HTML
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5.     <title>Untitled</title>
  6. </head>
  7.  
  8. <style>
  9. iframe {height:200px; width:300px; border:1px solid #000}
  10. </style>
  11.  
  12. <script>
  13. function iframeContent() {
  14. frame = document.getElementById("myFrame")
  15.  
  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>
  24.  
  25. <body>
  26.  
  27. <iframe src="document.html" id="myFrame">
  28. </iframe><br>
  29.  
  30. <button onclick="iframeContent()">OK</button>
  31.  
  32. </body>
  33. </html>
  34.  
IFRAME
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5.     <title>Untitled</title>
  6. </head>
  7.  
  8. <body>
  9.  
  10. <p id="test">Text</p>
  11.  
  12. </body>
  13. </html>
  14.  
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

3
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...
7
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. ...
1
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...
1
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...
1
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...
8
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....
2
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...
4
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...
9
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.