Iframe dynamic create and populate immediately

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Templar

    #1

    Iframe dynamic create and populate immediately

    Hi i'm bothering with such problem... I must dynamic create an Iframe, and
    then put som raw HTML into it. But I can't.

    When I create iframe, I can't access its properties.

    Here's the coe snipplet:

    //*************** *************** *******
    //*************** *************** *******

    function doTheIframe(naz wa, width, height) {
    ifrm = document.create Element("iframe ");
    ifrm.setAttribu te("name", nazwa);
    ifrm.setAttribu te("id", nazwa);
    ifrm.setAttribu te("src", "");
    ifrm.style.widt h.value = width+"px";
    ifrm.style.heig ht.value = height+"px";
    document.body.a ppendChild(ifrm );
    }

    function main() {
    doTheIframe('sm allframe',100,1 00);

    document.getEle mentById('small frame').content Window.document .body.innerHTML =
    "TEST";
    }

    <a onclick="main() ">Tratatata </a>

    //*************** *************** *******
    //*************** *************** *******

    When I click the link, iframe is created but not pupulated.
    When I click for the second time, second frame is created and the first one
    is populated.

    I want to populate the iframe immediately.
    How to do this?

    Regards,
    Templar




  • Martin Honnen

    #2
    Re: Iframe dynamic create and populate immediately



    Templar wrote:
    [color=blue]
    > Hi i'm bothering with such problem... I must dynamic create an Iframe, and
    > then put som raw HTML into it. But I can't.[/color]

    The following works for me with Netscape 7.1, IE6. Should work with
    Netscape 6/7 and IE5/5.5 too.
    I can't get it to work with Opera but I think I have filed a bug on that
    some while ago. You might want to try to load a dummy HTML page first to
    be able to script the document for Opera.

    <html>
    <head>
    <title>creati ng an iframe element and document.writin g content to the
    iframe window</title>
    <script type="text/javascript">
    function createIframe (iframeName, width, height) {
    var iframe;
    if (document.creat eElement && (iframe =
    document.create Element('iframe '))) {
    iframe.name = iframe.id = iframeName;
    iframe.width = width;
    iframe.height = height;
    iframe.src = 'about:blank';
    document.body.a ppendChild(ifra me);
    }
    return iframe;
    }

    function test () {
    var iframe = createIframe ('iframe0', 300, 300);
    if (iframe) {
    var iframeDoc;
    if (iframe.content Document) {
    iframeDoc = iframe.contentD ocument;
    }
    else if (iframe.content Window) {
    iframeDoc = iframe.contentW indow.document;
    }
    else if (window.frames[iframe.name]) {
    iframeDoc = window.frames[iframe.name].document;
    }
    if (iframeDoc) {
    iframeDoc.open( );
    iframeDoc.write (
    '<html><body><p >Kibology for all.<\/p><\/body><\/html>');
    iframeDoc.close ();
    }
    }
    }

    window.onload = function (evt) {
    test();
    }
    </script>
    </head>
    <body>
    <h1>Iframe creation test</h1>
    </body>
    </html>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Templar

      #3
      Re: Iframe dynamic create and populate immediately

      oh my goodness!! it works!!
      Thanks a million!

      But how?

      Regards,
      Templar


      Comment

      Working...