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?
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.
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.
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.
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.
var content=$("iframe").contents().find('body').html();
alert(content);
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With