Search code examples
javascriptjqueryiframeloadsrc

Loading portion of site into iframe


I have a script which loads content into a iframe using:

document.GetElementById('iframeid').src = 'site.html';

Now, the problem is that I want only a portion of the page loaded. If I could use jQuery I would specify which part by:

$('#iframeid').load(src = 'site.html .classforportion');

However if seems like jQuery .load doesn't work for iframes.

Is it possible to 'fool' the browser into grabbing the contents of a variable or a function for the first case? Or is it possible to create temporary 'html'-files only for so long that the script would load the right contents into it and pass it to an iframe (hm... seems unlikely...).

A work around solution for this specific case would be removing the first line of the loaded page. Is that possible with javascript or jQuery? Is it possible for this situation involving the iframe?

Thanks, going crazy over this thing! So, any help appreciated!


Solution

  • Ive never heard of a way of doing this with javascript since you cannot access files outside your own domain with javascript. You can do like this with PHP

    <?php
    function get_content_part($part=null, $website = null) 
    {
    $website = file_get_contents($website);
    $data = str_replace("\n", '', $website);
    echo preg_match("/\<div class\=\"'.$part.'\"\>(.*)<\/div\>/",$data,$match);
    return $match;
    }
    ?>
    

    Maybe integrate this with a javascript Ajax call and you can do it by javascript calls. Best regards Jonas