【问题标题】:iframe not loading its source on specific kind of pages: XML and Chrome pagesiframe 未在特定类型的页面上加载其源代码:XML 和 Chrome 页面
【发布时间】:2018-03-08 22:51:59
【问题描述】:

我正在开发一个 Chrome 扩展程序,为了向我的用户显示一些信息,我创建了一个 IFrame 并将其添加到页面的 DOM。

Iframe 包含在我的扩展中定义的模板。

到目前为止,在大多数页面上都没有问题,除非我尝试在特定页面上打开我的扩展程序:

  • 像 RSS Feeds 这样的 XML 文档 ==> 我真的需要解决这个问题
  • 特定的 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 Feed)时,我真的需要能够加载我的扩展程序。这是我扩展的重点之一。


附加信息:

我的扩展与 Firefox 兼容,实现完全相同。行为有点不同。当 XML 文档是 RSS 提要时,我可以打开 mmy 扩展,当它是别的东西时:这是不可能的。

示例链接:

一定有一些 XMLViewer 特有的东西,但我不知道在哪里寻找......

【问题讨论】:

  • 试试iframe = document.createElementNS('http://www.w3.org/1999/xhtml', 'iframe')iframe.setAttributeNS(null, 'style', '/* your CSS here */')
  • @wOxxOm:你摇滚!那行得通。我不能将您的 awswer 标记为好。您能否将其复制/粘贴为正常答案;)非常感谢。

标签: javascript html google-chrome iframe google-chrome-extension


【解决方案1】:

XML 文档需要您创建的元素的命名空间:

var iframe = document.createElementNS('http://www.w3.org/1999/xhtml', 'iframe');

属性应该是这样设置的:

iframe.setAttributeNS(null, 'style', '/* your CSS here */');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多