Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > window getElementByID works in IE but not in FireFox --- Help!

Reply
Thread Tools

window getElementByID works in IE but not in FireFox --- Help!

 
 
Derek Erb
Guest
Posts: n/a
 
      04-13-2005
I am banging my head against the wall with this one. The following
code snippets work perfectly fine in MSIE6. But produce an error in
Firefox and do not work at all.

BROWSER.HTM
<HTML>
....
<div class="Abb">
<h2 id="ABTit">BROWSER</h2>
</div>
....
</HTML>

function MyFun() {
var aWin = window.open(strURL, 'BROWSERWIN',
"dependent=yes,menubar=no,resizable=yes,scrallbars =yes,toolbar=no,left=100,top=100,width=500,height= 400");

aWin.document.getElementById('ABTit').innerHTML='M YTEXT';
}

My INDEX.HTM file calls the MyFun function which opens the BROWSER.HTM
file. The MyFun function is trying to modify the BROWSER.HTM window
contents and style.

In FireFox this produces the following error in the JavaScript Console
"aWin.document.getElementById("ABTit") has no properties".

I've seen that one before and normally it means I made a spelling error
somewhere... But this time it works fine in MSIE 6 and not in Firefox.

aWin.document.writeln("TESTING") works fine in both browsers.

I've tried various combinations and various web sites. I'm stumped...

Help!

 
Reply With Quote
 
 
 
 
Fred Oz
Guest
Posts: n/a
 
      04-13-2005
Derek Erb wrote:
> I am banging my head against the wall with this one. The following
> code snippets work perfectly fine in MSIE6. But produce an error in
> Firefox and do not work at all.
>
> BROWSER.HTM
> <HTML>
> ...
> <div class="Abb">
> <h2 id="ABTit">BROWSER</h2>
> </div>
> ...
> </HTML>
>
> function MyFun() {
> var aWin = window.open(strURL, 'BROWSERWIN',
> "dependent=yes,menubar=no,resizable=yes,scrallbars =yes,toolbar=no,left=100,top=100,width=500,height= 400");
>
> aWin.document.getElementById('ABTit').innerHTML='M YTEXT';
> }
>
> My INDEX.HTM file calls the MyFun function which opens the BROWSER.HTM
> file. The MyFun function is trying to modify the BROWSER.HTM window
> contents and style.
>
> In FireFox this produces the following error in the JavaScript Console
> "aWin.document.getElementById("ABTit") has no properties".
>
> I've seen that one before and normally it means I made a spelling error
> somewhere... But this time it works fine in MSIE 6 and not in Firefox.
>
> aWin.document.writeln("TESTING") works fine in both browsers.
>
> I've tried various combinations and various web sites. I'm stumped...


The most likely reason is that ABTit does not exist when
document.getElementById() tries to get it - the new window is still
creating the document.

--
Fred
 
Reply With Quote
 
 
 
 
RobB
Guest
Posts: n/a
 
      04-14-2005
Derek Erb wrote:
> I am banging my head against the wall with this one. The following
> code snippets work perfectly fine in MSIE6. But produce an error in
> Firefox and do not work at all.
>
> BROWSER.HTM
> <HTML>
> ...
> <div class="Abb">
> <h2 id="ABTit">BROWSER</h2>
> </div>
> ...
> </HTML>
>
> function MyFun() {
> var aWin = window.open(strURL, 'BROWSERWIN',
>

"dependent=yes,menubar=no,resizable=yes,scrallbars =yes,toolbar=no,left=100,top=100,width=500,height= 400");
>
> aWin.document.getElementById('ABTit').innerHTML='M YTEXT';
> }
>
> My INDEX.HTM file calls the MyFun function which opens the

BROWSER.HTM
> file. The MyFun function is trying to modify the BROWSER.HTM window
> contents and style.
>
> In FireFox this produces the following error in the JavaScript

Console
> "aWin.document.getElementById("ABTit") has no properties".
>
> I've seen that one before and normally it means I made a spelling

error
> somewhere... But this time it works fine in MSIE 6 and not in

Firefox.
>
> aWin.document.writeln("TESTING") works fine in both browsers.
>
> I've tried various combinations and various web sites. I'm

stumped...
>
> Help!


All window.open() does is 1) open a new window, 2) set its location to
the specified url, and 3) return the new window object. The last -
which allows any subsequent code to run - is not necessarily
synchronous with the new document loading and being available for
scripting. One way to avoid this is to load the new document as a
javascript: url (favelet): as this is loaded into window.location, like
a file, it always works. Depends on how elaborate the document is, and
whether there needs to be any preprocessing of it back at the server.
Here's a sample:

Put something like this in the opener window:

function getDoc(myText)
{
return [

'<html><head><title>Yo!</title></head>' ,
'<body style="background:.........' ,
'<div class="Abb"><h2 id="ABTit">' ,
myText ,
'</h2></div>........etc..........'

].join('');
}

Then:

var aWin = null;
function MyFun(myText)
{
aWin = window.open(
'javascriptpener.getDoc("' + myText + '")',
'BROWSERWIN',

'menubar=no,resizable=yes,scrollbars=yes,toolbar=n o,left=100,top=100,width=500,height=400'
);
}

..........

MyFun('MYTEXT');

 
Reply With Quote
 
Derek Erb
Guest
Posts: n/a
 
      04-14-2005
Thank you for your reply.

I don't understand why the window would be open in MSIE but still be
creating the document in Firefox.

However, regardless, is there some way to make my code wait until the
window is created and the document is ready so that I am certain I can
modify it?

Thank you in advance for your assistance.

 
Reply With Quote
 
Fred Oz
Guest
Posts: n/a
 
      04-14-2005
Derek Erb wrote:
> Thank you for your reply.
>
> I don't understand why the window would be open in MSIE but still be
> creating the document in Firefox.


Because once the script has created the window, it has finished its
job and moves on. The window then proceeds to load the document at
its own pace.

As for why one browser has finished creating the document and the
other hasn't - browser inconsistency is one of the fundamental
attractions of web programming.

>
> However, regardless, is there some way to make my code wait until the
> window is created and the document is ready so that I am certain I can
> modify it?
>


Read RobB's post below.



--
Fred
 
Reply With Quote
 
DU
Guest
Posts: n/a
 
      04-17-2005
Derek Erb wrote:
> I am banging my head against the wall with this one. The following
> code snippets work perfectly fine in MSIE6. But produce an error in
> Firefox and do not work at all.
>
> BROWSER.HTM
> <HTML>
> ...
> <div class="Abb">
> <h2 id="ABTit">BROWSER</h2>
> </div>
> ...
> </HTML>
>
> function MyFun() {
> var aWin = window.open(strURL, 'BROWSERWIN',
> "dependent=yes,menubar=no,resizable=yes,scrallbars =yes,toolbar=no,left=100,top=100,width=500,height= 400");


var aWin:
Creating a local reference to the window object is not recommendable. As
soon as you're outside the scope of the function, that reference is
useless. The only way you can overcome the asynchronous problem of
window creating and document loaded and functional is to use a global
variable to store the window reference and then use it from the opener
to modify your node.

dependent=yes: will not work in MSIE 6
scrallbars=yes: will not render scrollbars if needed, if content
overflows window dimensions in all browsers

>
> aWin.document.getElementById('ABTit').innerHTML='M YTEXT';


Poor usage of innerHTML: you have no node to insert, just a text node.
It's much better to use DOM 2 CharacterData attribute or just setting
the nodeValue directly. It's not only using web standards, it's not only
supported by MSIE 5+, Mozilla-based browsers, Safari 1.x, Opera 7.x,
Konqueror 3.x,etc. but it is also more efficient than innerHTML and it
is not a W3C DOM attribute.

DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Mozilla 1.7.7
 
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
Works in IE but not in firefox (not sure if syntax is correct) chrisgibson Firefox 0 08-26-2006 04:01 AM
Works in Firefox, but not in IE - pop up window - what did I do wrong? johnsonholding@yahoo.com Javascript 8 01-13-2006 07:17 AM
document.all[txtObj] works but document.getElementById[txtObj] fails wk Javascript 5 04-22-2005 01:11 PM
getElementById finds an element in IE but not Firefox Dave Hammond Javascript 9 02-16-2005 09:14 AM
Code works in Netscape 7.1 but not IE ---- document.getElementById('other').style.visibility='hidden'; lawrence Javascript 3 09-30-2004 06:22 AM



Advertisments