Accessing body of an iframe, works in ie not in mozilla.

Discussion in 'Javascript' started by Greg, Jan 23, 2005.

  1. Greg

    Greg Guest

    Javascript Folks,

    I am relatively new to javascript and am working on a script where I
    want to utilizes a couple of iframes and format text in them. I have
    run into a problem with getting the following code to work in mozilla.
    It seems to work in ie.

    Eventually I will be replacing the H1 node with a table until I can get
    the H1 node to show up in mozilla there is not much point.

    I have tested using both a javascript created iframe and one built in
    the html file. In both cases the iframes have an id of something_ifr.

    function lblifrBdy(id) {
    var ifr = document.getElementById(id+'_ifr');
    if (ifr) {
    alert ('found1: '+ifr.id+' '+ifr.nodeName);
    var ifrDoc = ifr.contentWindow ? ifr.contentWindow.document
    : ifr.contentDocument;
    var ifrbdy =
    ifrDoc.documentElement.getElementsByTagName'body').item(0);
    if (ifrbdy) {
    alert ('found ifrbdy: '+ifrbdy.nodeName);
    ifrbdy.id = id+'_ifrbdy';
    var heading = ifrDoc.createElement("H1");
    var heading_text = ifrDoc.createTextNode("Test!");
    heading.appendChild(heading_text);
    ifrbdy.appendChild(heading);
    }
    }
    }

    I have done a number of searches and so far have not come up with a
    solution. Any help you can provide would be greatly appreciated.
    Thanks,

    Greg
    Greg, Jan 23, 2005
    #1
    1. Advertising

  2. Greg wrote:



    > I have
    > run into a problem with getting the following code to work in mozilla.
    > It seems to work in ie.



    > function lblifrBdy(id) {
    > var ifr = document.getElementById(id+'_ifr');
    > if (ifr) {
    > alert ('found1: '+ifr.id+' '+ifr.nodeName);
    > var ifrDoc = ifr.contentWindow ? ifr.contentWindow.document
    > : ifr.contentDocument;
    > var ifrbdy =
    > ifrDoc.documentElement.getElementsByTagName'body').item(0);

    ^^^^^^^^^^
    I don't think that works, not even in IE.
    As for accessing the document inside an iframe the best cross browser
    approach is to use
    <iframe name="iframeName" ...></iframe>
    and then script
    var iframeDoc;
    if (window.frames && window.frames.iframeName &&
    (iframeDoc = window.frames.iframeName.document)) {
    // now use iframeDoc here
    }

    If that doesn't work for you or you want to improve the code that you
    have then tell us exactly where it goes wrong with Mozilla, what does
    its JavaScript console show you as script errors?

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 24, 2005
    #2
    1. Advertising

  3. Greg

    Greg Guest

    Oops, not sure what happened with the post. The actual javascript file
    reads:

    ifrDoc.documentElement.getElementsByTagName('body').item(0)­;

    somewhere along the way the ( got dropped.

    Modified the function based on the reply:

    > var ifrName = id+'_ifr';
    > var iframeDoc;
    > if (window.frames && window.frames[ifrName] &&
    > (iframeDoc = window.frames[ifrName].document)) {
    > var ifrbdy = iframeDoc.body;
    > if (ifrbdy) {
    > alert ('found ifrbdy');
    > ifrbdy.id = id+'_ifrbdy';
    > var heading = ifrDoc.createElement("H1");
    > var heading_text = ifrDoc.createTextNode("Test!");
    > heading.appendChild(heading_text);
    > ifrbdy.appendChild(heading);
    > }
    > }


    After making the modifications above I end up with the same results,
    works in IE but not in Mozilla. In IE I get Test! in the iframe.
    In Mozilla I don't get any text in the iframe and I don't get any
    errors in the javascript console window.

    Thanks,

    Greg
    Greg, Jan 29, 2005
    #3
  4. Greg

    Greg Guest

    Folks,

    This is getting worse not better. I must be missing something.
    The code below should create three iframes, put Test! in each
    iframe and then put Below Test under the three iframes.

    IE: It appears to work. However, it requires that the line
    "alert('continue')" be present which seems like there must be a timing
    problem. I am out of luck putting the code in the onload routine as it
    cannot find the body of the iframes.

    Mozilla: It does not work as I don't get any text in the iframes. Also
    there are no errors logged to the javascript console.

    Any help would be greatly appreciated.

    Thanks,

    Greg

    <html>
    <head>

    <script type="text/javascript">
    <!--
    function crifr (name, id, height, width, scroll) {
    var ifr;
    var bdy;
    var h1;
    var text;

    ifr = document.createElement('iframe');
    ifr.name = name+'_ifr';
    ifr.id = id+'_ifr';
    ifr.height = height;
    ifr.width = width;
    ifr.frameBorder = '1';
    ifr.marginHeight = '0';
    ifr.marginWidth = '0';
    ifr.scrolling = scroll;
    ifr.onload = test(name);

    return ifr;
    }

    function test(name) {
    var ifrDoc;
    var ifrbdy;
    var ifrName = name+'_ifr';
    alert('iframe: '+ifrName+' loaded');
    if (window.frames && window.frames[ifrName] &&
    (ifrDoc = window.frames[ifrName].document) &&
    (ifrbdy = window.frames[ifrName].document.body)) {
    alert ('in test, found ifrs body by name using: '+ifrName);
    }
    }

    function H1ifrBdy(name) {
    var ifrName = name+'_ifr';
    var ifrDoc;
    var ifrbdy;
    if (window.frames && window.frames[ifrName] &&
    (ifrDoc = window.frames[ifrName].document) &&
    (ifrbdy = window.frames[ifrName].document.body)) {
    alert ('in lblifrBdy, found ifrbdy using: '+ifrName);
    var heading = ifrDoc.createElement("H1");
    var heading_text = ifrDoc.createTextNode("Test!");
    heading.appendChild(heading_text);
    ifrbdy.appendChild(heading);
    }
    }

    function mytest () {
    var ifr1 = crifr('test1', 'test1', '100', '100', 'no');
    var ifr2 = crifr('test2', 'test2', '100', '100', 'no');

    var bdy = document.getElementsByTagName('body').item(0);

    bdy.appendChild(ifr1);
    bdy.appendChild(ifr2);

    alert('continue');

    H1ifrBdy('test1');
    H1ifrBdy('test2');
    H1ifrBdy('test3');
    }
    // -->
    </script>

    <title>Test</title>
    </head>

    <body id="bdymain">
    <iframe frameborder="1" height="100" id="test3_ifr" name="test3_ifr"
    width="100" onload="test('test3')"></iframe><br>
    <script type="text/javascript">
    <!--
    mytest();
    // -->
    </script>

    <h1>Below Test</h1>
    </body>
    </html>
    Greg, Jan 29, 2005
    #4
    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. Henri
    Replies:
    9
    Views:
    1,811
    Toby Inkster
    Feb 22, 2005
  2. London Boy
    Replies:
    2
    Views:
    313
    Keith Bowes
    Jan 12, 2004
  3. Daedalous
    Replies:
    3
    Views:
    658
    Daedalous
    Jan 16, 2004
  4. Replies:
    5
    Views:
    197
    A. Sinan Unur
    Sep 8, 2005
  5. Robert Oschler
    Replies:
    2
    Views:
    284
    cosmic foo
    Jul 29, 2005
Loading...

Share This Page