Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from iframe

I am doing this first time. I have created an iframe on my page and I want the text from the iframe through jquery. Here is my code :

<html>
  <head><script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

    <script type="text/javascript">
    function copyIframeContent(iframe){  
        var iframeContent = $(iframe).contents(); //alert(iframeContent);
        //$("#result").text("Hello World");
        $("#result").html(iframeContent.find('body').html);alert(iframeContent.find('body').html());
    }
    </script>
  </head>
  <body>
    <iframe id="myIframe" onload="copyIframeContent(this);" name="myIframe" src="text.php"></iframe><br />
    Result:<br />
    <textarea id='result'></textarea>
    <input type="button" value="click" id="btn" onclick="aa()">
    <script type="text/javascript">
         function aa(){  alert("Fdf");
            alert(document.getElementById('myIframe').contentWindow.document.body.innerHTML);
    }
    </script>
  </body>
</html>

text.php:

text to change

I tried a lot in all browsers but still this is not working. Can anyone help me to get this content?

like image 709
deck john Avatar asked May 16 '13 13:05

deck john


People also ask

Can you scrape data from iframe?

Any content that can be viewed on a webpage can be scraped. Period. Some sites make it a bit trickier than others, but as long as you're making the right HTTP request and parsing the response, you should be able to see the exact same information in your web scraping program that you see on the screen in your browser.

How do I access elements in an iframe?

Getting the element in Iframeconst iframe = document. getElementById("myIframe"); Now, it has and contentWindow property which returns the document object by using that we can access the elements from an Iframe.

How do I transfer data from iframe to parent?

Sending some data from the child iframe to the parent window is also pretty simple. Whenever you embed an iframe, the iframe will have a reference to the parent window. You just need to use the PostMessage API to send data via the window. parent reference of the parent window.

What is #document in iframe?

Definition and Usage. The contentDocument property returns the Document object generated by a frame or iframe element. This property can be used in the host window to access the Document object that belongs to a frame or iframe element.


3 Answers

var content=$("iframe").contents().find('body').html();

alert(content);
like image 128
Sudhakar Singajogi Avatar answered Oct 25 '22 08:10

Sudhakar Singajogi


The contentWindow works in both FF and chrome

document.getElementById('myFrame').contentWindow.document.body    

Would give you a DOM element body

You can also try something like

 window.frames['myIframe'].document.body    

That might do the trick for you also

You might have problems with your browsers built in security. If you run this on a local machine. There is a way to disable browsers security.

like image 33
John b Avatar answered Oct 25 '22 09:10

John b


Use .contents() to get to iFrame's DOM.

$('#myIframe').contents()

UPDATE:

In the OP:

$("#result").html(iframeContent.find('body').html);

Should say:

$("#result").html(iframeContent.find('body').html());
like image 35
Petr Vostrel Avatar answered Oct 25 '22 09:10

Petr Vostrel