简体   繁体   中英

Using Iframe to load content from another HTML

I need to display content(present in another HTML) in an iframe inside an already present HTML page. The iframe should first check if there is some content in the HTML, and only load if there is, if the source HTML is empty the iframe should not load.

Thank You

To check wether the content is present in the iframe use this(it uses jQuery and let the id of iFrame be iframeid ):

   <!--free.htm -->
    <!DOCTYPE html>
    <html>
      <head>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script>
         $(document).ready(function(){
             $.get("test.htm", function(){
                if($(this).contents().find("body").length<=0)  
                    $("#iframeid").css("display","none");       
             });
         });
         </script>
       </head>

       <body>
         <iframe src="test.htm" id="iframeid"></iframe>
       </body>
</html>

Here are dome links that might be of help to you :

You'll have to use Javascript for this.

Your parent HTML file:

<html>
  <!-- Your parent html file contents -->
  <iframe id="iframe" src="">

  </iframe>

  <script src="jquery-1.11.2.min.js"></script>
  <script>
    $(document).ready(function() {
      //get your iFrame html file
      $.get("iframe.html", function(data) {
          //check if file not empty
          if(data) {
             $("#iframe").attr("src", "iframe.html");
          } else {
             $("#iframe").css("display", "none");
          } 
      });
    });
  </script>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM