javascript - How to get document object of iframe? - Stack Overflow

i have a a website that displays an iframe for specific information. Both of them e from the same domai

i have a a website that displays an iframe for specific information. Both of them e from the same domain. Therefore any blocks for cross-site scripting should not be an issue.

The HTML Page inside the Iframe now uses some JavaScript to alter the DomElement depending on specific users actions I try to change the css. Therefore I do following JavaScriptCode:

var oldlink = document.getElementsByTagName("link").item(1);
    
var newlink = document.createElement("link");
newlink.setAttribute("rel", "stylesheet");
newlink.setAttribute("type", "text/css");
newlink.setAttribute("href", cssFile);
    	
document.getElementsByTagName("head").item(0).replaceChild(newlink, oldlink);

i have a a website that displays an iframe for specific information. Both of them e from the same domain. Therefore any blocks for cross-site scripting should not be an issue.

The HTML Page inside the Iframe now uses some JavaScript to alter the DomElement depending on specific users actions I try to change the css. Therefore I do following JavaScriptCode:

var oldlink = document.getElementsByTagName("link").item(1);
    
var newlink = document.createElement("link");
newlink.setAttribute("rel", "stylesheet");
newlink.setAttribute("type", "text/css");
newlink.setAttribute("href", cssFile);
    	
document.getElementsByTagName("head").item(0).replaceChild(newlink, oldlink);

This works fine in standalone. Problem is now when it is embedded in an iframe. I now get the document from the main/parent html document an not the one from the own (child) document. Does anyone know why this is? It seems to work in Firefox but not in Chrome and IE.

As it looks up the "wrong" documet I'll get an Error in the DeveloperTools that states: The node to be replaced is not a child of this node.

I know it can't find the Node, since it's not in the parent document but in the child document.

Anyone an Idea on how I can solve this or what I need to look up? The only stuff i found is how to access parent or child documents, this is actually the behaviour I don't want.

Thansk for any idea

Share Improve this question edited Oct 15, 2015 at 9:29 dsharew 10.7k6 gold badges54 silver badges76 bronze badges asked Oct 13, 2015 at 15:44 eagleeagle 632 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Not so much clear what you want but if you want to update elements inside the frame you need to replace 'document' by iframe document like this:

var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

Then your code will be like this:

var oldlink = innerDoc.getElementsByTagName("link").item(1);

var newlink = innerDoc.createElement("link");
newlink.setAttribute("rel", "stylesheet");
newlink.setAttribute("type", "text/css");
newlink.setAttribute("href", cssFile);

innerDoc.getElementsByTagName("head").item(0).replaceChild(newlink, oldlink);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744706263a4589074.html

相关推荐

  • javascript - How to get document object of iframe? - Stack Overflow

    i have a a website that displays an iframe for specific information. Both of them e from the same domai

    19小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信