1

I want to dynamically populate an iframe with Javascript.

I do not want the iframe in a html document loaded in via src.

I do not want to use Jquery, just plain old javascript (ECMA 5.1).

Here is my code so far:

var elementToAttachTo = document.getElementById(attachTo);

// Need to create and attach iframe to page before populating.
// attachTo is provided as a parameter to this function, this definitely
// works as I got this working setting the iframe src to a html file..

var ifrm = document.createElement("iframe");
ifrm.setAttribute("id", "blabla");
// I set src to about:blank here, cause I read to do that somewhere...
ifrm.setAttribute("src", "about:blank");

// I attach the iframe BEFORE populating it.
elementToAttachTo.appendChild(ifrm);

ifrm = populateIframe(ifrm);

Here is my populateIframe function:

function populateIframe(iframe) {
    var ifrmHtml = iframe.contentDocument.documentElement;

    var p = document.createElement("p");
    p.innerHTML = "Hello iframe!";
    ifrmHtml.appendChild(p);

    iframe.contentDocument.documentElement = ifrmHtml;
    return iframe;
}

I was trying to call appendChild() directly to the iframe which didn't work, so I did some research and found examples of appending to the contentDocument.documentElement. It still doesn't work.

Many thanks for any help!

14
  • 1
    Is there a particular reason why you are using an iFrame rather than a div? Commented Jan 5, 2018 at 16:36
  • I think iframes can only be populated from the src, if you are just appending elements, then I would use a div as robin says Commented Jan 5, 2018 at 16:39
  • Yeah, the iframe must be hosted as it will process credit card data Commented Jan 5, 2018 at 16:39
  • 1
    Unless it is hosted on your server, I don't think you'll be able to tamper with it, otherwise anyone would be able to inject stuff into iframes to rob people's details Commented Jan 5, 2018 at 16:40
  • We're not worried about people tampering with the iframe, as it is hosted by us. Hence the use of an iframe. Users of the SDK will not be PCI compliant. Commented Jan 5, 2018 at 16:43

1 Answer 1

1

You missed to append iframe content to the body.

function populateIframe(iframe) {
    var ifrmHtml = iframe.contentDocument.documentElement.querySelector('body');

    console.log(ifrmHtml)

    var p = document.createElement("p");
    p.innerHTML = "Hello iframe! Ja sam faca";
    ifrmHtml.appendChild(p);

    iframe.contentDocument.documentElement = ifrmHtml;
    return iframe;
}

See here --> https://jsfiddle.net/nmitic/24ejkfnw/

Sign up to request clarification or add additional context in comments.

5 Comments

4 hours well spent. Thank you sir!
Yeah, I hate when that happens :D
I've been through at least 4-5 resources and examples of this issue and none of them included the body, so I never thought of it. In my original example I actually did add a body first and add divs to it, it musn't have been working for another reason. I'll just have to use TDD properly I guess and add a few lines at a time. Thanks again.
I didn't get why you code is not working, but then I inspect the dom and saw that p tag gets rendered under the body, so that's when the light turn on for me.
Annoyingly, I can't inspect the Iframe DOM, as when I try "Inspect Frame Source" it comes up as blank, I assume because it doesn't have a 'src' set. Even now with the text showing, it is blank. Very annoying for testing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.