Iframe dynamic create and populate immediately

Discussion in 'Javascript' started by Templar, Feb 12, 2004.

  1. Templar

    Templar Guest

    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(nazwa, width, height) {
    ifrm = document.createElement("iframe");
    ifrm.setAttribute("name", nazwa);
    ifrm.setAttribute("id", nazwa);
    ifrm.setAttribute("src", "");
    ifrm.style.width.value = width+"px";
    ifrm.style.height.value = height+"px";
    document.body.appendChild(ifrm);
    }

    function main() {
    doTheIframe('smallframe',100,100);

    document.getElementById('smallframe').contentWindow.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
     
    Templar, Feb 12, 2004
    #1
    1. Advertising

  2. Templar wrote:

    > 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.


    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>creating an iframe element and document.writing content to the
    iframe window</title>
    <script type="text/javascript">
    function createIframe (iframeName, width, height) {
    var iframe;
    if (document.createElement && (iframe =
    document.createElement('iframe'))) {
    iframe.name = iframe.id = iframeName;
    iframe.width = width;
    iframe.height = height;
    iframe.src = 'about:blank';
    document.body.appendChild(iframe);
    }
    return iframe;
    }

    function test () {
    var iframe = createIframe ('iframe0', 300, 300);
    if (iframe) {
    var iframeDoc;
    if (iframe.contentDocument) {
    iframeDoc = iframe.contentDocument;
    }
    else if (iframe.contentWindow) {
    iframeDoc = iframe.contentWindow.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/
     
    Martin Honnen, Feb 12, 2004
    #2
    1. Advertising

  3. Templar

    Templar Guest

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

    But how?

    Regards,
    Templar
     
    Templar, Feb 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Stevens
    Replies:
    1
    Views:
    351
    Curt_C [MVP]
    Dec 28, 2004
  2. vulkanino
    Replies:
    0
    Views:
    3,510
    vulkanino
    Dec 16, 2004
  3. subba_reddy
    Replies:
    2
    Views:
    1,054
    Edwin Knoppert
    Jun 22, 2006
  4. Enjoy Life
    Replies:
    2
    Views:
    283
    Roland Hall
    Feb 23, 2005
  5. Daedalous
    Replies:
    3
    Views:
    725
    Daedalous
    Jan 16, 2004
Loading...

Share This Page