最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamic iframe not loaded occasionally in Firefox - Stack Overflow

programmeradmin0浏览0评论

I have some JavaScript code that dynamically injects an iframe in a given HTML page. Unfortunately, in Firefox, and only in Firefox, although the iframe is created from time to time the relevant URL isn't loaded into it.

I know it wasn't loaded because the relevant URL doesn't appear in the Firebug Net tab, and when I inspect the iframe I don't see any expected HTML code in there (when the iframe is on the same domain as the outlying page). I don't see any JavaScript or network errors either.

Here's a code snippet, I've checked all the relevant variables are correct:

    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;

Here's an example URL that is set for the iframe (the issue also recreates when the URL points to an external server, had to omit the 'http://' at the beginning otherwise I couldn't post the question):

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

Doing some research on the web, I found this unfixed Firefox bug which seems related to this issue: .cgi?id=279048

After reading the bug, I tried several solutions none of which solved the issue:

  • Setting iframe.src instead of iframe.contentWindow.location
  • Adding a random parameter to the querystring
  • Adding the '#' symbol with a random number at the end of the URL
  • Giving the iframe a random name

Does anyone have a workaround for this annoying Firefox bug? Or is the issue I'm describing unrelated to the bug and has a different solution?

I have some JavaScript code that dynamically injects an iframe in a given HTML page. Unfortunately, in Firefox, and only in Firefox, although the iframe is created from time to time the relevant URL isn't loaded into it.

I know it wasn't loaded because the relevant URL doesn't appear in the Firebug Net tab, and when I inspect the iframe I don't see any expected HTML code in there (when the iframe is on the same domain as the outlying page). I don't see any JavaScript or network errors either.

Here's a code snippet, I've checked all the relevant variables are correct:

    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;

Here's an example URL that is set for the iframe (the issue also recreates when the URL points to an external server, had to omit the 'http://' at the beginning otherwise I couldn't post the question):

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

Doing some research on the web, I found this unfixed Firefox bug which seems related to this issue: https://bugzilla.mozilla/show_bug.cgi?id=279048

After reading the bug, I tried several solutions none of which solved the issue:

  • Setting iframe.src instead of iframe.contentWindow.location
  • Adding a random parameter to the querystring
  • Adding the '#' symbol with a random number at the end of the URL
  • Giving the iframe a random name

Does anyone have a workaround for this annoying Firefox bug? Or is the issue I'm describing unrelated to the bug and has a different solution?

Share Improve this question edited May 2, 2013 at 21:35 Azd325 6,1405 gold badges36 silver badges57 bronze badges asked Nov 17, 2010 at 14:29 Ido SchachamIdo Schacham 3402 silver badges12 bronze badges 3
  • I don't think that Firefox bug is related to this at all. – Pointy Commented Nov 17, 2010 at 14:36
  • 1 Oh also - install the Firefox "TamperData" plugin and see if you can tell whether an HTTP request is being made for the frame. – Pointy Commented Nov 17, 2010 at 14:37
  • When this issue reproduces, there is no HTTP request for that frame. When it doesn't reproduce, then there is an HTTP request. – Ido Schacham Commented Nov 17, 2010 at 15:19
Add a ment  | 

2 Answers 2

Reset to default 5

What happens if you add this to the bottom of your script?

iframe.contentWindow.location.reload(true);

Perhaps it will stop the need to reload in FF.

EDIT

Fixed the example

Solved the issue, I was looking in the wrong place. The HTML file where this dynamic iframe was loaded had an empty iframe tag that was removed from the DOM, after which the dynamic iframe was injected instead.

Apparently Firefox cached the last URL for this iframe, and loaded it immediately as the external page loaded. I know because I saw the relevant HTML file being loaded twice in the Firebug Net tab rather than once upon the injection.

After I got rid of this empty iframe tag and relied only on the injected iframe, everything started to work well and the issue didn't reproduce anymore. I guess Firefox didn't like handling this scenario, some kind of bug maybe?

Thanks anyway for helping me out, it gave me the inspiration for the right solution :)

发布评论

评论列表(0)

  1. 暂无评论