Can't load iFrame CSS

2019-02-27 13:16发布

问题:

I'm trying to load an iFrame into a Wordpress website. The iFrame should load the table displayed in this link, with all the colors and other styles applied.

However, when I insert the iFrame on my website, it will load the content of the table but not the CSS.

This is how I tried to 'force' load the right CSS, as I suspect the Wordpress theme is overriding the Table CSS:

<script language="javascript" type="text/javascript">
    function iFrameHeight() {
var h = 0;
if (!document.all) {
    h = document.getElementById('blockrandom').contentDocument.height;
    document.getElementById('blockrandom').style.height = h + 60 + 'px';
} else if (document.all) {
    h = document.frames('blockrandom').document.body.scrollHeight;
    document.all.blockrandom.style.height = h + 20 + 'px';
    var cssLink1 = document.createElement("link");
    cssLink1.href = "https://www.gscris.it/lmo/lmo-style.css";
    cssLink1.rel = "stylesheet";
    cssLink1.type = "text/css";
    frames['iframe'].document.head.appendChild(cssLink1);
    var cssLink2 = document.createElement("link");
    cssLink2.href = "https://www.gscris.it/lmo/lmo-style-nc.css";
    cssLink2.rel = "stylesheet";
    cssLink2.type = "text/css";
    frames['iframe'].document.head.appendChild(cssLink2);
    }
}
    </script>
    <div>Campioni d'amicizia</div>
        <iframe onload="iFrameHeight()" id="blockrandom" name="iframe" src="https://www.gscris.it/lmo/lmo.php?todo=&file=Piccamici201819.l98" width="100%" height="600" scrolling="no" align="top" frameborder="0">
    Questa scelta non funziona correttamente in quanto il browser utilizzato non supporta gli Inline Frames     </iframe>

What am I doing wrong? This is my website page where the iframe is inserted (scroll to the bottom).

回答1:

Use HTTPS to serve your css in the iframe. If you open the Chrome Devtools, the following error message can be seen multiple times: Mixed Content: The page at '<URL>' was loaded over HTTPS, but requested an insecure image '<URL>'. This content should also be served over HTTPS.



回答2:

First of all Iframes will not take the external css. as they have there own html head and body tags, Its a separate html document. you cannot override any styles in Iframe. instead you can try attach your stylesheet in the required document and use it in the iframe.