admin管理员组文章数量:1186450
I have a PHP page with a few iFrames that cause the page to load fairly slowly. How do I go about only loading those iFrames once the page has completely loaded? I assume I need to use JavaScript but I haven't yet found a code that works.
Many thanks.
EDIT - The iFrame needs to appear within a PHP echo.
Here's the code:
<?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='=<?=$i[Postcode]?>'><?=$i[Postcode]?></a><
<p>
<?=$i[ContactName]?><br>
<?=$i[TelephoneNumber]?>
</div>
<p>
<a href='.php?Employer=<?=$i[Employer]?>'>
Edit this entry
</a>
<br>
//HERE IS WHERE I WANT TO LOAD THE iFRAME
</p>
</div>
</div>
</div>
<?php
}
?>
I have a PHP page with a few iFrames that cause the page to load fairly slowly. How do I go about only loading those iFrames once the page has completely loaded? I assume I need to use JavaScript but I haven't yet found a code that works.
Many thanks.
EDIT - The iFrame needs to appear within a PHP echo.
Here's the code:
<?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
}
?>
Share
Improve this question
edited Apr 30, 2012 at 9:08
Phil Howell
asked Apr 30, 2012 at 8:45
Phil HowellPhil Howell
711 gold badge1 silver badge4 bronze badges
3
- use jquery $(document).ready(function(){ //add code here }); – Philemon philip Kunjumon Commented Apr 30, 2012 at 8:48
- nczonline.net/blog/2009/09/15/iframes-onload-and-documentdomain – Grunf Commented Apr 30, 2012 at 8:50
- Hmmm - that's not working. It's within a MySQL echo - is that an issue? – Phil Howell Commented Apr 30, 2012 at 8:52
4 Answers
Reset to default 13As @Sudhir and @Richard showed, you can solve this problem by deferring the load of iframe
contents and it just takes a few javascript lines.
Sometimes, though, is more convenient to place the iframe
element into the source code instead of creating the element at runtime. Well, you can have it too, by initially creating the iframe
element pointing to about:blank
and changing its source at runtime:
<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>
Edited for @PhilHowell: I hope the idea is clearer now.
Try adding at end of your body:
window.onload = function() {
var iframe = document.createElement('iframe');
iframe.style.display = "none";
iframe.src = your_iframe_source_url;
document.body.appendChild(iframe);
};
If you want to, you can load your iframes once the page has loaed like this:
I have used jquery for simplicity here:
$(function() {
$('<iframe />').attr('src', 'http://www.google.com').appendTo('#yourelementId');
});
see more here:
http://jquery-howto.blogspot.co.uk/2010/02/dynamically-create-iframe-with-jquery.html
JS Iframe loader
So for PHP Echo:
<?php
echo "<div id='yourelementId'></div><script>$(function() { $('<iframe />').attr('src', 'http://www.google.com').appendTo('#yourelementId');});</script>";
?>
Oh and make sure you have linked jQuery in or use the code that Sudhir has given.
you can lazy load the iframe
<iframe src="someSrc.com" loading="lazy"></iframe>
本文标签: javascriptLoad iFrame after page loadStack Overflow
版权声明:本文标题:javascript - Load iFrame after page load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738356707a2079970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论