0

Is there any possible workaround to get the html of iframe that contains content from another domain and do something with JS/jQuery depending on what does it contain? AFAIK browsers hide that content due to the same origin policy so I can't just directly access it.

yxfxmx
  • 665
  • 1
  • 6
  • 13
  • i am not sure but you can try sending a `Access-Control-Allow-Origin` header which white-list the domain – Cerlin Jul 13 '15 at 07:07

1 Answers1

0

If you do not want to include a whole plugin via iFrame, which already uses a lot of scripts, and the site is more like for example a table with results from a local sports club or other "read only" information, you can use php and file_get_contents(), to import the iframe. Now you are able to stylize its contents and target them via jQuery.

For example you have got the following markup:

<html>
<!-- Some Head and Body parts, like navigation, sidebar, etc. -->
<div id="results">
<!-- Lots of results -->
</div>
</html>

You can target id="result to identify the start of your section to import.

So lets define your target:

$host = 'http://sportclub.de/results.html';

and import him:

$filestring = file_get_contents($host);

As you do not want to import the whole url, you need to set the starting and ending point. To give you a basic idea, this is a code, that works somehow like the concept named above:

<?php
$host = 'http://sportclub.de/results.html';
$filestring = file_get_contents($host);

$startpos = 0; //erstes Zeichen
while($pos = strpos($filestring, "<div id=\"results\"", $startpos))
{
    $string = substr($filestring, $pos, strpos($filestring, "</div>", $pos + 1) - $pos);
    echo $string."</div>";
    $startpos = $pos + 1;
}
$string = preg_replace("/(^¦\s)(http:\/\/)?(www\.)?[\.0-9a-zA-Z\-_~]+\.(com¦net¦org¦info¦name¦biz¦.+\.\w\w)((\/)?[0-9a-zA-Z\.\-_~#]+)?\b/ie","",$string);
?>
Marian Rick
  • 3,350
  • 4
  • 31
  • 67
  • And what if I have no access to back end, if I only have that iframe content "as is" and need to find out if it contains something that I'm looking for? All I need is read-only access to iframe's DOM to check the innerHTML of certain element – yxfxmx Jul 13 '15 at 07:22
  • @yxfxmx you mean that you are not able to influence how the iframe is embedded into a site, you are only able to edit a javascript file? Or you are not able to edit the content of the iframe itself (which will be embedded via, as said, iframe)? – Marian Rick Jul 13 '15 at 08:08