7

我有一个带有一些 iFrame 的 PHP 页面,这些 iFrame 导致页面加载速度相当慢。页面完全加载后,如何仅加载这些 iFrame?我假设我需要使用 JavaScript,但我还没有找到有效的代码。

非常感谢。

编辑 - iFrame 需要出现在 PHP 回显中。

这是代码:

<?php
            while($i = mysql_fetch_array($e))
            {
        ?>

    <div class='dhtmlgoodies_question'>
        <div style='float:left'><img src='../images/categories/<?=$i[Category]?>.png'></div>
        <div class='name'><?=$i[Name]?></div>
        <div class='location'><?=$i[Area]?></div>
    </div>

    <div class='dhtmlgoodies_answer'>
    <div>

        <div style='background-color:#FFF; margin-left:248px; padding:10px; color:#666'>
        <?=$i[Address]?><br>
        <?=$i[Area]?><br>
        <a href='http://maps.google.com/maps?q=<?=$i[Postcode]?>'><?=$i[Postcode]?></a><
        <p>
        <?=$i[ContactName]?><br>
        <?=$i[TelephoneNumber]?>
        </div>
        <p>
        <a href='http://crbase.phil-howell.com/view.php?Employer=<?=$i[Employer]?>'>
        Edit this entry
        </a>
        <br>

    //HERE IS WHERE I WANT TO LOAD THE iFRAME


    </p>
        </div>

    </div>
</div>
        <?php
            }
        ?>
4

3 回答 3

13

正如@Sudhir 和@Richard 所展示的,您可以通过延迟加载iframe内容来解决这个问题,并且只需要几行javascript 行。

不过,有时将iframe元素放入源代码比在运行时创建元素更方便。好吧,您也可以拥有它,通过最初创建iframe指向about:blank并在运行时更改其源的元素:

<html>
<head>
<script>
    function loadDeferredIframe() {
        // this function will load the Google homepage into the iframe
        var iframe = document.getElementById("my-deferred-iframe");
        iframe.src = "./" // here goes your url
    };
    window.onload = loadDeferredIframe;
</script>
</head>    
<body>
    <p>some content</p>

    <!-- the following iframe will be rendered with no content -->
    <iframe id="my-deferred-iframe" src="about:blank" />

    <p>some content</p>
</body>
</html>

为@PhilHowell 编辑:我希望这个想法现在更清楚了。

于 2012-04-30T10:07:55.833 回答
7

尝试在你的身体末端添加:


window.onload = function() {
    var iframe = document.createElement('iframe');
    iframe.style.display = "none";
    iframe.src = your_iframe_source_url;
    document.body.appendChild(iframe);
};
于 2012-04-30T08:53:15.557 回答
3

如果您愿意,您可以在页面加载后加载您的 iframe,如下所示:

为了简单起见,我在这里使用了 jquery:

$(function() {

   $('<iframe />').attr('src', 'http://www.google.com').appendTo('#yourelementId'); 

});

在这里查看更多:

http://jquery-howto.blogspot.co.uk/2010/02/dynamically-create-iframe-with-jquery.html

JS iframe 加载器

所以对于 PHP Echo:

<?php

  echo "<div id='yourelementId'></div><script>$(function() { $('<iframe />').attr('src', 'http://www.google.com').appendTo('#yourelementId');});</script>"; 

?>

哦,请确保您已将 jQuery 链接到或使用 Sudhir 提供的代码。

于 2012-04-30T08:54:06.270 回答