开发者

Getting CSS from parent on an iFrame

开发者 https://www.devze.com 2022-12-22 16:50 出处:网络
I am using a phpbb3 forum with a bbcode to insert an iframe. I want to use the css from the forum for the iframe. I tried some code I found here but it isn\'t working. I might be doing something wrong

I am using a phpbb3 forum with a bbcode to insert an iframe. I want to use the css from the forum for the iframe. I tried some code I found here but it isn't working. I might be doing something wrong but I'd love to get this working, especially since 开发者_StackOverflow中文版users can change the forum css. The forum is: botdls.prophbb.com and the iframe is the "shoutbox" at the top. Thanks. Here is the code I used from this site which I found from google.

var cssLink = document.createElement("link") 
            cssLink.href = "pFstylesEditor.css"; 
            cssLink .rel = "stylesheet"; 
            cssLink .type = "text/css"; 

            //Instead of this
            //frames['frame1'].document.body.appendChild(cssLink);
            //Do this

            var doc=document.getElementById("edit").contentWindow.document 

            ///If you are doing any dynamic writing do that first
            doc.open;
            doc.write(myData);
            doc.close();

            //Then append child
            doc.body.appendChild(cssLink);


I spotted one problem right away. You're creating an element in the parent document and trying to append it to the child document - this won't work. You need to create the element using the child frame's document instead:

var doc=document.getElementById("edit").contentWindow.document;

var cssLink = doc.createElement("link");
cssLink.href = "pFstylesEditor.css";  
cssLink.rel = "stylesheet";  
cssLink.type = "text/css";  

///If you are doing any dynamic writing do that first 
doc.open();        // <-- you also missed the parenthesis here
doc.write(myData); 
doc.close(); 

//Then append child 
doc.body.appendChild(cssLink); 

Also, open is a method and you missed off the parenthesis (open()).

0

精彩评论

暂无评论...
验证码 换一张
取 消