I have an iframe
and I want to get the head content value.
<iframe>
<html>
<head>
<style>body{color:red;}</style>
<link type="text/css" rel="stylesheet" href="some link">
</head>
</html>
</iframe>
I tried this script:
var sett_wped_head = jQuery('iframe').contents().find("head").text();
alert(sett_wped_head);
but it returns body{color:red;}
. my goal to get value is
<style>body{color:red;}</style>
<link type="text/css" rel="stylesheet" href="some link">
is it possible?
You could use . contents() method of jQuery. The . contents() method can also be used to get the content document of an iframe, if the iframe is on the same domain as the main page.
contentWindow. document. body. innerHTML: It returns the HTML content of iframe body.
You can read the contents in an iframe easily via jQuery ( $(iframe). contents() ) if the domain of the webpage which contains the iframe is same as the domain of the web-page opened in iframe . The microservice responsible for uploading file was on different domain and the main app is on another domain.
Answer: Use the jQuery contents() method You can use the jQuery contents() method in combination with the find() , val() and html() methods to insert text or HTML inside an iframe body.
You need to use html()
for getting html content inside
var sett_wped_head = jQuery('iframe').contents().find("head").html();
// ------^------
alert(sett_wped_head);
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