Ajouter un stylesheet à iframe

Eléphant du PHP | 80 Messages

11 mai 2007, 17:41

Comment ajouter un stylesheet à un objet iframe dans Firefox ?

Le code ci-dessous marche dans IE (applique le style dans iframe comme dans la page principale), par contre sous Firefox, ajouter un stylesheet à un iframe n'a aucun effet (l'arrière plan ne change pas de couleur, la police de caractères reste Times New Roman...)

PS. A remarquer toutefois : Firebug affiche bien qu'un <link ... /> approprié est ajouté à iframe.

main.html

Code : Tout sélectionner

<html> <head> <script> function AddCSS(fwin) { // Adding style sheet var styles = "main.css"; var newSS=fwin.document.createElement('link'); newSS.rel='stylesheet'; newSS.type='text/css'; newSS.href=escape(styles); fwin.document.getElementsByTagName("head")[0].appendChild(newSS); // Adding some text var sometext = fwin.document.createElement('div'); sometext.className = 'redtext'; sometext.innerHTML = 'Hello <u>World</u>!'; fwin.document.getElementsByTagName("body")[0].appendChild(sometext); // Adding some embedded style var embedstyle = fwin.document.createElement('style'); embedstyle.type='text/css'; embedstyle.innerHTML = 'body { color:green; }'; fwin.document.getElementsByTagName("body")[0].appendChild(embedstyle); } </script> </head> <body> <iframe id="iView" style="width:500px; height:200px"></iframe> <div onclick="AddCSS(document.getElementById('iView').contentWindow);">Add style to iframe</div> <div onclick="AddCSS(window);">Add style to this window</div> <hr /> Explication of the problem: when we add a text ("Hello <u>World</u>!") or an inner style element (green text), it works for both the window and the iframe object. But adding a stylesheet works only for the window, and fails for iframe. Note that in DOM, <link ...> element specifying the stylesheet appears in both the window and the iframe. </body> </html>
main.css

Code : Tout sélectionner

body { padding:20px; font-family:'Trebuchet MS', sans-serif; background-color:orange; }

Eléphant du PHP | 100 Messages

14 mai 2007, 12:33

Une fois l'attribut "src" définit dans la balise iframe, ton code marche bien chez moi ... (Firefox 2.0.0.3)

Code : Tout sélectionner

<iframe id="iView" style="width:500px; height:200px" srcv="iframe.html"></iframe>
Script gratuit de videothèque: PhpMesFilms
sur http://phpmesfilms.dyndns.org/

Eléphant du PHP | 80 Messages

14 mai 2007, 12:48

D'accord ; il suffit donc juste d'ajouter une page vide à iframe.

Merci beaucoup pour votre aide !