1

I'm creating an iframe and when I add it to the dom it does not load the url. I actually have to right click it and hit refresh...any reason for this to happen? I'm using this code on a CWE extension for Lync, that runs on an embedded browser but it's actually IE7.

Here's the code: (the url it's fine because hitting refresh load the right page so i did not included the to get the url)

var iframe = document.createElement('iframe');
iframe.id = "myframe";
iframe.height = "400";
iframe.width = "700";
iframe.style.border = "0";
iframe.scrolling = "no";
iframe.frameBorder = "0";
iframe.style.display = "block";
iframe.setAttribute("src", this.GetUrl(x));

main.appendChild(iframe);
nhenrique
  • 874
  • 1
  • 16
  • 35

1 Answers1

4

Try:

frame.src = this.GetUrl(x);

instead of:

iframe.setAttribute("src", this.GetUrl(x));

frame.src is the property, while iframe.setAttribute("src", this.GetUrl(x)); sets the attribute.

To avoid inconsistencies and problems with old browsers, try appendChild first so that the DOM is aware of its existence before setting the src.

    var iframe = document.createElement('iframe');
    iframe.id = "myframe";
    iframe.height = "400";
    iframe.width = "700";
    iframe.style.border = "0";
    iframe.scrolling = "no";
    iframe.frameBorder = "0";
    iframe.style.display = "block";
  //  iframe.setAttribute("src", this.GetUrl(x));

    main.appendChild(iframe);
    frame.src = this.GetUrl(x);
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • it's still happening the same issue, I have cleared the cache just to be sure too :) – nhenrique Dec 12 '13 at 14:03
  • @nhenrique: try `frame.document.location.href = this.GetUrl(x)` – Khanh TO Dec 12 '13 at 14:19
  • I tried that one but it's still happening. I noticed something..if i put an alert after appending the iframe, this gets loaded the first time as expected..without the alert, the iframe does not load automatically. Something interrupting the loading process of the iframe? Never faced an issue like this before – nhenrique Dec 12 '13 at 14:25
  • Try experimenting with **display** CSS attribute. Append with 'display: none' and then set to default (block?). – Jacek Krysztofik Dec 12 '13 at 14:30
  • 1
    @nhenrique: that's strange, what if you `appendChild` first and then set the `src` – Khanh TO Dec 12 '13 at 14:32
  • that actually works! btw, do you have any idea why? thank you! update your answer so i can mark it as correct :) – nhenrique Dec 12 '13 at 14:35
  • @nhenrique: I'm also not sure why. This could be a browser problem. Different browsers have different behaviors. – Khanh TO Dec 12 '13 at 14:49