Funky function assignment question (Firefox vs. IE)

C

Csaba2000

I thought assigning one function to another was fairly straightforward,
as the example below illustrates. So I was really surprised that IE 6
showed the alert while Firefox 1.0.1 failed.

It's easy to shore up Firefox by changing the elem=... line, below, to:
var elem = function (myId) {return document.getElementById(myId);}
Could any of the theory gurus shed some light on this discrepancy?


<body id=myBody onLoad="tryit()">
<script type='text/javascript'>
var elem = document.getElementById; // just say no to long function
names
function tryit() { alert (elem('myBody').id); }
</script></body>


Csaba Gabor from New York
 
M

Michael Winter

Csaba2000 said:
I thought assigning one function to another was fairly straightforward,

Assigning one "function" to another is, however this may not be true
with "methods". A method may depend on being called as a method of its
parent object:

var gEBI = document.getElementById;
gEBI(id); // fail
gEBI.call(document, id); // success

As I think the above demonstrates, the code used by Firefox depends
upon the this operator referring to the document object.

[snip]
Could any of the theory gurus shed some light on this discrepancy?

This isn't a discrepancy - just a design decision/implementation
side-effect. There is nothing inherently correct nor incorrect in
either Microsoft's or Mozilla's approach.

[snip]
var elem = document.getElementById; // just say no to long function
names

Be sure to control wrapping when posting code. It's a good idea to
always wrap code to 70 characters or so.

Mike
 
L

Lee

Csaba2000 said:
I thought assigning one function to another was fairly straightforward,
as the example below illustrates. So I was really surprised that IE 6
showed the alert while Firefox 1.0.1 failed.

It's easy to shore up Firefox by changing the elem=... line, below, to:
var elem = function (myId) {return document.getElementById(myId);}
Could any of the theory gurus shed some light on this discrepancy?


<body id=myBody onLoad="tryit()">
<script type='text/javascript'>
var elem = document.getElementById; // just say no to long function
names
function tryit() { alert (elem('myBody').id); }
</script></body>

In Firefox, getElementById searches for the id within
the document of which the function is an attribute.

You create window.elem, which is not an attribute of
any document.
 
R

RobG

Csaba2000 wrote:
[...]
<body id=myBody onLoad="tryit()">
<script type='text/javascript'>
var elem = document.getElementById; // just say no to long function
names
function tryit() { alert (elem('myBody').id); }
</script></body>

I think a much better way of doing this (if it needs doing at
all) is:

var elem = function(id) {return document.getElementById(id)}

and call it with:

elem('anId');

However, if you wanted to make this more useful, then include
some of the dynWrite functionality from the group FAQ:

<URL:http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html>

var elem = function(id) {
if (document.getElementById){
return document.getElementById(id)
} else if (document.all) {
return document.all[id];
}
}
 
C

Csaba2000

Be sure to control wrapping when posting code. It's a good idea to always
wrap code to 70 characters or so.
Wow, thanks for those great replies Mike, Lee, and Rob. I learned something
new.

I hope you don't mind if I go off topic a bit. That wrap was an
oversight, but I may as well adjust my system right now. I'm
using (ahem) Outlook Express 6 until Thunderbird has its bugs
worked out (can't do wholesale ignores), and my current settings
(Tools/Options.../Send/Plain Text Settings...) are:
Message format: Uuencode / wrap text at 76 characters

This is not working for me (and hence not working for you).
So, should I set this to:
A) Message format: Uuencode / wrap text at 132 characters
B) Message format: MIME (quoted printable) / no word wrap setting
C) Neither is good, better to count characters

I'm only ever sending vanilla, plain text. And on my OE, options
A and B both show up just fine when I send longer lines. As a
matter of course, I am using the Enter key to format my own
text before sending, but I'd certainly appreciate being to have
wider width in my outgoing messages.

My real question is, does A or B negatively impact other
common newsreaders, or can I just choose one? I'd choose
B, I suppose (the benefit being that even if the post shows
wrapped in my reader, if I copy and paste to Notepad, it
retains the original lines), though it doesn't prefix quotations
(the post being responded to) with a '>' character.


Thanks,
Csaba
 
R

RobG

Csaba2000 wrote:
[...]
I hope you don't mind if I go off topic a bit. That wrap was an
oversight, but I may as well adjust my system right now. I'm
using (ahem) Outlook Express 6 until Thunderbird has its bugs
worked out (can't do wholesale ignores), and my current settings
(Tools/Options.../Send/Plain Text Settings...) are:
Message format: Uuencode / wrap text at 76 characters

This is not working for me (and hence not working for you).
So, should I set this to:
[...]

Dunno, I'm using Thunderbird 'cos it's cross-platform and, being
fairly new to usenet, the best I've come across. I tried all
sorts and whilst some swear by this agent or that, I found
Thunderbird best for me, despite its drawbacks.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top