简体   繁体   中英

Iframe created dynamically not loading

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM