473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

Jul 23 '05 #1
3 8314


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/
Jul 23 '05 #2
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

Jul 23 '05 #3
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>

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Frédéric Mayot | last post by:
Hi, I have an HTML page like this : --- test.html <html> <body> <form> <iframe id="frametest" name="frametest" src="iframe.html"></iframe> <select multiple...
1
by: R | last post by:
Hello, I have a frame in which there are 2 IFrames., both being loaded from the same domain. One IFrame is loaded from http://test1.xyz.com and the other IFrame is loaded from...
2
by: pgnPoster | last post by:
I am having the dickens of a time figuring this out. I've been googling high and low, but all the examples I find involve the iframe's src containing some HTML albeit minimal. I just want it to...
14
by: Aaron Gray | last post by:
Hi, I want to access the properties of an IFrame but seem unable to get access to the IFrames document body. <html> <body> <iframe src="test.html" id="IFrame"></iframe> </body>
3
by: Axel Gallus | last post by:
I wrote a javascript function which should return the html code between a frame's body tag: function load_frame_as_string( Framename ) { var Frame =...
5
by: theone3000 | last post by:
Hi All, I am trying to access a div in an iframe(the div is the first object within the body tag, no table, text, etc), i walked the dom tree in the "Dom Inspector" in mozilla, but cannot make sence...
3
by: judy.j.miller | last post by:
Does anyone know why i can't access a form element value using dot notation in firefox, when i'm in a function. Works ok in the body. I'm trying to do this: var FarTemp = faren.temp.value; I...
3
by: bulldog8 | last post by:
I am having problems with changing a frames source file for Mozilla (1.5.0.4). The followig code works for IE: <script type="text/javascript"> var imgP = new Image(); function...
8
by: removeps-groups | last post by:
Is there any way to control the space between top of browser or frame and document body? What I have is this: There is a simple file test1.html, and file test.html that displays test1.html in an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.