iframe不在特定类型的页面上加载其源代码:XML和Chrome页面
问题描述:
我正在开发Chrome扩展程序,为了向我的用户显示一些信息,我创建并向页面的DOM添加了IFrame。iframe不在特定类型的页面上加载其源代码:XML和Chrome页面
Iframe包含在我的扩展中定义的模板。
到目前为止,这个在大多数页面,除了当我尝试打开特定页面上的我的分机号问题:
- XML文档,例如RSS订阅==>我真的需要解决这个。
- 特定的Chrome页面==>我并不关心这个用例。
我创建并以下列方式,当用户打开我的分机管理的iframe:
var iframe = document.createElement('iframe');
iframe.id = "my_awesome_iframe";
// Then I specify the source: I ask chrome to look for it.
// I have checked the correct URL is returned. No problem so far here.
iframe.src = chrome.runtime.getURL("templates/page.html');
// Here the problems start:
// with a normal page: No problem
// with an XML document: I have the error: "style is undefined, can't set cssText"
iframe.style.cssText = 'height: 100% !important; width: 100% !important;';
// I can solve this doing the following:
iframe.setAttribute("style", 'height: 100% !important; width: 100% !important;');
// And finally
document.body.appendChild(iframe);
但现在,一切都正确设置。而且,无论何时我遇到上述情况之一,src url只是未加载。我有DOM内的iframe元素,但它只是空的。
只是为了检查,我试过如下:
document.getElementById("my_awesome_iframe").contentWindow
// As expected, I obtain the following only when the iframe refuses to load.
>> undefined
不知道如何解决这个问题?
我真的需要能够加载我的扩展,当用户在XML文件,如RSS源。这是我延伸的主要观点之一。
附加信息:
我的扩展是完全相同的实施与Firefox兼容。行为有点不同。当XML文档是一个RSS源时,我可以打开mmy扩展名,当它是别的东西时:这是不可能的。
实例链接:
- RSS订阅:https://www.feedcrunch.io/@dataradar/rss/(只适用于FF)
- XML文档:http://www.httrack.com/httrack.xml(在FF和Chrome失败)
必须有一些XMLViewer-具体的东西,但我不知道在哪里寻找...
答
XML文档要求名称创建步伐,为元素:
var iframe = document.createElementNS('http://www.w3.org/1999/xhtml', 'iframe');
和属性应该设置这样的:
iframe.setAttributeNS(null, 'style', '/* your CSS here */');
尝试'IFRAME =与Document.createElementNS('http://www.w3.org/1999/ xhtml','iframe')'和'iframe.setAttributeNS(null,'style','/ *你的CSS在这里* /')' – wOxxOm
@wOxxOm:你摇滚!这工作。我不能将你的爱好标志为好。你可以复制/粘贴它作为一个正常的答案;) 非常感谢。 –