【问题标题】:Make iframe load after mainpage inside php echo在 php echo 中的主页后加载 iframe
【发布时间】:2018-06-20 21:31:53
【问题描述】:

我有一个从 mysql 数据库加载内容的页面,我需要在主页之后加载 iframe,因为有超过 20 个 iframe,它会显着减慢页面速度。

<?php

$con = mysql_connect("localhost","####","####");

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("daniel_login", $con);

$result = mysql_query("SELECT * FROM replays ORDER BY id DESC");

while($row = mysql_fetch_array($result))

  {

  echo "<div class=\"col-sm-6 col-xs-12 masonry__item\" data-masonry-filter=\"" . $row['category'] . "\">";

  echo "<div class=\"portfolio-item portfolio-item-2 video-cover\" data-scrim-bottom=\"9\">";

  echo "<div class=\"background-image-holder\"><img src=\"../img/" . $row['image'] . "\" /></div>";

  echo "<div class=\"portfolio-item__title\"><h5>" . $row['h1'] . "</h5><span><em>" . $row['h2'] . "</em></span></div><div class=\"video-play-icon video-play-icon--sm\"></div>";

  echo "<div id=\"dna_video\"></div>";

  echo "<iframe src=\"" . $row['video'] . "?autoplay=0\" allowfullscreen=\"allowfullscreen\"></iframe></div></div>";

  }

mysql_close($con);

?>

我在网上试了很多脚本都没用。

【问题讨论】:

    标签: javascript php html dom iframe


    【解决方案1】:

    PHP 是在服务器端处理的,这意味着您不能告诉它的某些部分等到某些页面已加载到浏览器中。为此,您可以使用 Javascript(我在此示例中使用 jQuery):

    <script type="text/javascript">
    var links = [];
    
    <?php
    //Create DB connection $con - I use PDO
    $result = $con->query("SELECT * FROM replays ORDER BY id DESC");
    
    while($row = $result->fetch()){
        echo 'links.push("'.$row['video'].'");'; //get all urls
        //links.push("video-link");
    }
    ?>
    
    $(function(){ //wait until page loads
      for(var n=0;n<links.length;n++){
        $('body').append($('<iframe>').attr('src',links[n])); //loop through links and add to page as iframe
      }
    });
    </script>
    

    另外 - mysql_* 函数(例如 mysql_connect())已被弃用 - 您绝对应该切换到 PDO 或 mysqli。 (Why shouldn't I use mysql_* functions in PHP? )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多