Firefox中偶尔不会加载动态iframe

Ido*_*ham 6 html javascript iframe firefox dom

我有一些JavaScript代码,可以在给定的HTML页面中动态注入iframe.不幸的是,在Firefox中,只在Firefox中,虽然iframe是不时创建的,但相关的URL并没有加载到它中.

我知道它没有加载,因为相关的URL没有显示在Firebug Net选项卡中,当我检查iframe时,我没有看到任何预期的HTML代码(当iframe与外围的同一域时)页).我也没有看到任何JavaScript或网络错误.

这是一段代码片段,我检查了所有相关变量是否正确:

    var iframe = document.createElement("iframe");
    iframe.width = options["w"];
    iframe.height = options["h"];
    iframe.scrolling = "no";
    iframe.marginWidth = 0;
    iframe.marginHeight = 0;
    iframe.frameBorder = 0;
    iframe.style.borderWidth = 0;

    if (node.childNodes.length > 0) 
        node.insertBefore(iframe, node.childNodes[0]);
    else 
        node.appendChild(iframe);

    iframe.contentWindow.location = iframeSrc + "?" + querystring;
Run Code Online (Sandbox Code Playgroud)

这是为iframe设置的示例URL(当URL指向外部服务器时,该问题也会重新创建,必须在开头省略'http://',否则我无法发布问题):

127.0.0.1:8000/widget/iframe/index.html?style=slide-top-to-bottom&culture_code=en_us&c=26&sc=1324&title=Top%20News&caption=Top%20Stories&order=relevance&count=20&w=250&h=300&timestamp=true&scrollbar=false&theme=ui-lightness&className=8815455464592103&referrer=http%3A%2F%2F127.0.0.1%3A8000%2Fwidget%2Fbuilder%2Findex.html
Run Code Online (Sandbox Code Playgroud)

在网上做一些研究,我发现这个未修复的Firefox bug似乎与这个问题有关:https: //bugzilla.mozilla.org/show_bug.cgi?id = 279048

阅读完bug后,我尝试了几种解决方案,但没有解决问题:

  • 设置iframe.src而不是iframe.contentWindow.location
  • 将随机参数添加到查询字符串
  • 在URL的末尾添加带有随机数的"#"符号
  • 给iframe一个随机名称

有没有人有这个烦人的Firefox bug的解决方法?或者我描述的问题与错误无关并且有不同的解决方案?

Oli*_*cal 5

如果将其添加到脚本底部会发生什么?

iframe.contentWindow.location.reload(true);
Run Code Online (Sandbox Code Playgroud)

也许它会停止在FF中重新加载的需要.

编辑

修复了这个例子


Ido*_*ham 1

问题解决了,我找错地方了。加载此动态 iframe 的 HTML 文件有一个空的 iframe 标记,该标记已从 DOM 中删除,之后又注入了动态 iframe。

显然,Firefox 缓存了该 iframe 的最后一个 URL,并在加载外部页面时立即加载它。我之所以知道,是因为我看到相关的 HTML 文件在 Firebug Net 选项卡中加载了两次,而不是在注入时加载一次。

在我摆脱这个空的 iframe 标签并仅依赖于注入的 iframe 后,一切开始正常工作,并且问题不再重现。我猜 Firefox 不喜欢处理这种情况,也许是某种错误?

无论如何,感谢您帮助我,它给了我正确解决方案的灵感:)