views:

4001

answers:

7
<iframe id="id_description_iframe" class="rte-zone" height="200" frameborder="0" title="description">
<html>
<head>
</head>
<body class="frameBody">
test
<br/>
</body>
</html>
</iframe>

Here,what I want to get is :

test
    <br/>
A: 

I think placing text inbetween the tags is reserved for browsers that cant handle iframes i.e...

<iframe src ="html_intro.asp" width="100%" height="300">
  <p>Your browser does not support iframes.</p>
</iframe>

You use the 'src' attribute to set the source of the iframes html...

Hope that helps :)

Chalkey
A: 

Chalkey is correct, you need to use the src attribute to specify the page to be contained in the iframe. Providing you do this, and the document in the iframe is in the same domain as the parent document, you can use this:

var e = document.getElementById("id_description_iframe");
if(e != null) {
   alert(e.contentWindow.document.body.innerHTML);
}

Obviously you can then do something useful with the contents instead of just putting them in an alert.

Graham Clark
This only works in IE. It throws an error in FF.
ichiban
+3  A: 

AFAIK, an Iframe cannot be used that way. You need to point its src attribute to another page.

Here's how to get its body content using plane old javascript. This works with both IE and Firefox.

function getFrameContents(){
   var iFrame =  document.getElementById('id_description_iframe');
   var iFrameBody;
   if ( iFrame.contentDocument ) 
   { // FF
     iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
   }
   else if ( iFrame.contentWindow ) 
   { // IE
     iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
   }
    alert(iFrameBody.innerHTML);
 }
Jose Basilio
A: 

jQuery doesn't have a neat way to do this does it? Shame really...

Tim
A: 

Using JQuery, try $("#id_description_iframe").contents().find("body").html()

Michael Adedayo
A: 

The following code is cross-browser compliant. It works in IE7, IE8, Fx 3, Safari, and Chrome, so no need to handle cross-browser issues. Did not test in IE6.

<iframe id="iframeId" name="iframeId">...</iframe>

<script type="text/javascript">
    var iframeDoc;
    if (window.frames && window.frames.iframeId &&
        (iframeDoc = window.frames.iframeId.document)) {
        var iframeBody = iframeDoc.body;
        var ifromContent = iframeBody.innerHTML;
    }
</script>
nekno
it do not work in chrome
Codler
The html below worked for me in Chrome 7 on Mac. I tested this original post in Chrome 6 on Windows and it worked fine.<html><head></head><body><iframe id="iframeId" name="iframeId">...</iframe><script type="text/javascript">    var iframeDoc;     if (window.frames     }</script></body></html>
nekno
A: 

Nekno, your answer works great in both IE and Firefox. In my application I need to get the content of the iframe and then search for a particular word and scroll the iframe down to display the word if it is found. I display a list of job descriptions. The user can click any of them to see the actual discription. On the back button from the actual description I want to highlight the job description that was previously clicked so the user can see where they are. I have it working fine in IE but it won't highlight or scroll in Firefox.

Here is my code:

        var n = 0; 
    function FindinIframe1(id, jobName) {
               var txt, i, found; 
               var iframeDoc;
               if (window.frames && window.frames.DocFrame &&
                    (iframeDoc = window.frames.DocFrame.document)) {
                    var iframeBody = iframeDoc.body;
                    var ifromContent = iframeBody.innerHTML;
               }
                if (jobName == "") return false; 
                if (window.execScript) {  //IE
                   txt = iframeBody.createTextRange();
                   for (i = 0; i <= n && (found = txt.findText(jobName)) != false; i++) {
                      txt.moveStart("character", 1);
                      txt.moveEnd("textedit");
                   }
                   // If found, mark it and scroll it into view
                   if (found) {
                      txt.moveStart("character", -1);
                      txt.findText(jobName);
                      txt.select();
                      txt.scrollIntoView();
                      n++;
                   }
                   //Otherwise, start over at the top of the page and find first match
                   else {
                      if (n > 0) {
                         n = 0;
                         alert("End of document has been reached.");
                      }
                      //Not found anywhere, give message.
                      else
                         alert("No string found.");
                   }
                } else {  //Firefox
                   var startPos = ifromContent.indexOf(jobName);
                   if (startPos == -1)
                   {
                     alert('No string found');
                     return false;
                   }
                   //alert("highlight found text");
                   iframeBody.focus();
                   iframeBody.setSelectionRange(startPos, startPos + jobName.length);
                   return true;
                   if (n == 0)
                      alert ("No string found.");
                }
                return false;
             }

I simply call the function from my html like so:

    <body onload="FindinIframe1('DocFrame','JOB')">

and my iframe is defined as:

               <iframe 
                   id="DocFrame" name="DocFrame" frameborder="0" runat="server"
                   width="793" height="416" 
                   src="JobDescriptions.aspx"> </iframe>

Can anyone see what I am doing wrong? IE works perfectly. Firefox does not give me any errors but it also will not highlight or scroll to show the job description. If I add alerts, it shows that it executes the correct path through the code.

Brown