Javascript Iframe innerHTML

后端 未结 10 2484
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 07:08

Does anyone know how to get the HTML out of an IFRAME I have tried several different ways:

document.getElementById(\'iframe01\').contentDocument.body.innerHT         


        
相关标签:
10条回答
  • 2020-11-27 07:44
    document.getElementById('iframe01').outerHTML
    
    0 讨论(0)
  • 2020-11-27 07:44

    You can get html out of an iframe using this code iframe = document.getElementById('frame'); innerHtml = iframe.contentDocument.documentElement.innerHTML

    0 讨论(0)
  • 2020-11-27 07:47

    Having something like the following would work.

    <iframe id = "testframe" onload = populateIframe(this.id);></iframe>
    
    // The following function should be inside a script tag
    
    function populateIframe(id) { 
    
        var text = "This is a Test"
    var iframe = document.getElementById(id); 
    
    var doc; 
    
    if(iframe.contentDocument) { 
        doc = iframe.contentDocument; 
    } else {
        doc = iframe.contentWindow.document; 
    }
    
    doc.body.innerHTML = text; 
    
      }
    
    0 讨论(0)
  • 2020-11-27 07:47

    This solution works same as iFrame. I have created a PHP script that can get all the contents from the other website, and most important part is you can easily apply your custom jQuery to that external content. Please refer to the following script that can get all the contents from the other website and then you can apply your cusom jQuery/JS as well. This content can be used anywhere, inside any element or any page.

    <div id='myframe'>
    
      <?php 
       /* 
        Use below function to display final HTML inside this div
       */
    
       //Display Frame
       echo displayFrame(); 
      ?>
    
    </div>
    
    <?php
    
    /* 
      Function to display frame from another domain 
    */
    
    function displayFrame()
    {
      $webUrl = 'http://[external-web-domain.com]/';
    
      //Get HTML from the URL
      $content = file_get_contents($webUrl);
    
      //Add custom JS to returned HTML content
      $customJS = "
      <script>
    
          /* Here I am writing a sample jQuery to hide the navigation menu
             You can write your own jQuery for this content
          */
        //Hide Navigation bar
        jQuery(\".navbar\").hide();
    
      </script>";
    
      //Append Custom JS with HTML
      $html = $content . $customJS;
    
      //Return customized HTML
      return $html;
    }
    
    0 讨论(0)
  • 2020-11-27 07:50

    I think this is what you want:

    window.frames['iframe01'].document.body.innerHTML 
    

    EDIT:

    I have it on good authority that this won't work in Chrome and Firefox although it works perfectly in IE, which is where I tested it. In retrospect, that was a big mistake

    This will work:

    window.frames[0].document.body.innerHTML 
    

    I understand that this isn't exactly what was asked but don't want to delete the answer because I think it has a place.

    I like @ravz's jquery answer below.

    0 讨论(0)
  • 2020-11-27 07:55

    Don't forget that you can not cross domains because of security.

    So if this is the case, you should use JSON.

    0 讨论(0)
提交回复
热议问题