Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery how to get iframe head value content

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?

like image 702
gadss Avatar asked Nov 23 '15 05:11

gadss


People also ask

Can you access the content of an iframe?

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.

How do I get iframe innerHTML?

contentWindow. document. body. innerHTML: It returns the HTML content of iframe body.

How do I read Cross domain iframe content?

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.

How can I get iframe HTML code using jQuery?

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.


1 Answers

You need to use html() for getting html content inside

var sett_wped_head = jQuery('iframe').contents().find("head").html(); 
//                                                       ------^------
alert(sett_wped_head);
like image 75
Pranav C Balan Avatar answered Oct 16 '22 20:10

Pranav C Balan