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

G

Greg

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
 
M

Martin Honnen

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?
 
G

Greg

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
 
G

Greg

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top