-1

Is it possible to get HTML of an iframe (please note that iframe is refering to a webpage that loads via ajax call) from the page where I am loading it via ajax call using jquery?

Actualy I want to scrap the content of this website. The issue is that this website loads via ajax. So I can not use curl for getting contents of a web page that require javascript to load.

For the solution of this issue I am calling a php file with ajax from index.php and passing my query q=blog to the php page that returns this iframe to index.php

<iframe id="myframe" src="https://blekko.com/#?q=blog" width="100%" height="100%"></iframe>

After successful ajax response I am saving the response of ajax that is an iframe refering to the website along with query in a div in index.php

    <div id="myhtml" style="display:none"></div>

After 3 to 6 seconds contents of this website load in the iframe of index.php I have a jquery function in index.php that checks html of div id "myhtml" after 5 seconds of interval

<javascript>
var newInt = setInterval(function(){ check(); }, 5000);
function check(){
blekko_html = $("#myframe").contents().find("html").html();
alert(blekko_html);
}
</javascript>

But each time when check() function calls, it returns / alert this

<iframe id="myframe" src="https://blekko.com/#?q=blog" width="100%" height="100%"></iframe>

Even if the iframe is loaded with the content of webpage it always returns above iframe's html initialization, not the html of the webpage inside it.

Is it possible to get the html of iframe that is loading via ajax call and that iframe is containing a webpage that loads via ajax call?

if I able to get the html of that iframe then I will send it again to a php page using ajax were I can do scraping and fetch required data.

Mamoon Rashid
  • 109
  • 3
  • 8
  • @SteAp Actualy not. My question is diffirent...we can get the content of an iframe using jquery...but how to get the content of an iframe that we are calling via ajax and also webpage inside the iframe loads after few seconds of ajax response...because content inside the frame loads via ajax. – Mamoon Rashid Aug 31 '13 at 00:36

3 Answers3

2

You are trying to access content of iframe which points to webpage from another domain.

You cannot access content of iframe if the src of that iframe is not pointing to the domain on which your current parent page is. This is called cross domain policy

You will have to use server side language that will grab the html of given url and return it to your index page to display in any div or whatever.

Let me give you an example to explain why javascript cannot have cross domain access.

  • Suppose i have FB like box on my website inside an iframe
  • now whenever any user comes on my website i will trigger a click to the like box which is inside the iframe of like box.
  • This way my FB page will have lakhs of likes.
  • But because of cross domain policy this cannot be done.

hope you got my point

Ronak Patel
  • 2,570
  • 17
  • 20
  • You are right...that is why I am getting this error in console Permission denied to access property 'document' Source File: code.jquery.com/jquery-1.7.2.min.js?_=1377911359134 Line: 3. My first priority was server side language, like php to grab the html...but the webpage that I need to scrap and get it's HTML is loading via ajax and all server side methods like CURL does not support javascript... – Mamoon Rashid Aug 31 '13 at 14:04
1

you need to make sure the iframe content is loaded first

function check(){
    $("#myframe").load(function(){
          blekko_html = $("#myframe").contents().find("html").html();
          alert(blekko_html);
    });
}
Liam Allan
  • 1,115
  • 1
  • 8
  • 13
0

maybe add onload to the iframe and check there

like

<iframe id="myframe" src="https://blekko.com/#?q=blog" width="100%" height="100%" onload="check();"></iframe>

and I would modify the check() to do console.log(blekko_html) instead of alert,

then check in developer console in Chrome or in Firebug or in IE F12

Jacek Pietal
  • 1,980
  • 1
  • 18
  • 27
  • I have tried this and got this message from console Error: Permission denied to access property 'document' Source File: http://code.jquery.com/jquery-1.7.2.min.js?_=1377911359134 Line: 3 ... any idea that how to get access? – Mamoon Rashid Aug 31 '13 at 01:13