Funky function assignment question (Firefox vs. IE)

Discussion in 'Javascript' started by Csaba2000, Mar 6, 2005.

  1. Csaba2000

    Csaba2000 Guest

    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
     
    Csaba2000, Mar 6, 2005
    #1
    1. Advertising

  2. Csaba2000 wrote:

    > 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

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Mar 6, 2005
    #2
    1. Advertising

  3. Csaba2000

    Lee Guest

    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.
     
    Lee, Mar 6, 2005
    #3
  4. Csaba2000

    RobG Guest

    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];
    }
    }


    --
    Rob
     
    RobG, Mar 7, 2005
    #4
  5. Csaba2000

    Csaba2000 Guest

    "Michael Winter" <> wrote in message
    news:X4LWd.29925$...

    > 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
     
    Csaba2000, Mar 7, 2005
    #5
  6. Csaba2000

    RobG Guest

    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.

    --
    Rob
     
    RobG, Mar 7, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Diogenes Dilone

    Funky error Situation

    Diogenes Dilone, Jan 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    550
    Diogenes Dilone
    Jan 7, 2004
  2. =?Utf-8?B?VHJhY3k=?=

    Funky address change.

    =?Utf-8?B?VHJhY3k=?=, Mar 16, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    394
    Juan T. Llibre
    Mar 16, 2006
  3. iksrazal

    funky but simple regex

    iksrazal, Jul 19, 2004, in forum: Java
    Replies:
    3
    Views:
    403
    iksrazal
    Jul 20, 2004
  4. Pete
    Replies:
    7
    Views:
    430
  5. Denis Remezov

    Funky function

    Denis Remezov, Jun 24, 2004, in forum: C++
    Replies:
    4
    Views:
    450
    Karl Heinz Buchegger
    Jun 25, 2004
Loading...

Share This Page