Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Accessing body of an iframe, works in ie not in mozilla.

Reply
Thread Tools

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

 
 
Greg
Guest
Posts: n/a
 
      01-23-2005
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-24-2005


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/
 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      01-29-2005
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

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      01-29-2005
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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To reduce your body weight & slim your body Loss weight MCSA 0 07-23-2007 07:54 PM
To reduce your body weight & slim your body Loss weight MCSA 0 07-21-2007 05:15 AM
When I turn on my PC, it works, works, works. Problem! Fogar Computer Information 1 01-17-2006 12:57 AM
Not detecting body.scrollTop and body.scrollLeft in IE6 London Boy Javascript 2 01-12-2004 08:44 AM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM



Advertisments