Is there a cross-domain iframe height auto-resizer that works?

后端 未结 7 2397
暖寄归人
暖寄归人 2020-11-27 09:41

I tried a few solutions but wasn\'t successful. I\'m wondering if there is a solution out there preferably with an easy-to-follow tutorial.

7条回答
  •  孤城傲影
    2020-11-27 10:27

    I found another server side solution for web dev using PHP to get the size of an iframe.

    First is using server script PHP to an external call via internal function: (like a file_get_contents with but curl and dom).

    function curl_get_file_contents($url,$proxyActivation=false) {
        global $proxy;
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
        curl_setopt($c, CURLOPT_REFERER, $url);
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
        if($proxyActivation) {
            curl_setopt($c, CURLOPT_PROXY, $proxy);
        }
        $contents = curl_exec($c);
        curl_close($c);
        $dom = new DOMDocument();
        $dom->preserveWhiteSpace = false;
        @$dom->loadHTML($contents);
        $form = $dom->getElementsByTagName("body")->item(0);
        if ($contents) //si on a du contenu
            return $dom->saveHTML();
        else
            return FALSE;
    }
    $url = "http://www.google.com"; //Exernal url test to iframe
    
        
        
        
        
    
        
            

    You need to display under the div (iframe_reserve) the html generated by the function call by using a simple echo curl_get_file_contents("location url iframe","activation proxy")

    After doing this a body event function onload with javascript take height of the page iframe just with a simple control of the content div (iframe_reserve)

    So I used divHeight = document.getElementById("iframe_reserve").clientHeight; to get height of the page external we are going to call after masked the div container (iframe_reserve). After this we load the iframe with its good height that's all.

提交回复
热议问题