Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post

    pointer to document methods throw exception in moz

    in moz/nn, this errors
    Code:
    var el = document.getElementById;
    why?

    pointers to core classes and/or singletons seem fine; this works for example
    Code:
    var ord = String.fromCharCode;
    alert(ord(100));

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Not enough code and rather confusing. Can you detail a little bit? What does not work and what error do you get?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    when referencing method pointer in mozilla/nn, this error occurs:
    Code:
    Error: uncaught exception: [Exception... "Illegal operation on WrappedNative prototype object"  nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)"  location: "JS frame :: anonymous :: line 9"  data: no]
    this only happens when referencing document or window methods - e.g., the following fail:
    Code:
    var a = document.getElementById;
    alert(a("bob").tagName);
    var b = document.getElementsByTagName;
    alert(b("p").length);
    these work fine
    Code:
    var a = String.fromCharCode;
    alert(a(100));
    var b = Math.round;
    alert(b(12.34234));
    var c = Array.prototype.sort;
    alert(c.call([1,"a","z","m",10]));
    var d = {e:{f:{g:{h:{i:function(args){return args;}}}}}};
    var j = d.e.f.h.i;
    alert(j("..."));
    i'm not looking for a solution - i'd like to know why, purely for edification.

    tyia.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    var a = document.getElementById
    and
    var b = document.getElementsByTagName
    can not be objects, as long as you have not gave them valid id's and names.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    My educated guess:

    When you do var x = document.getElementById and then x("hello") you are executing the function x in the context of the window object instead of the document object. Gecko probably utilizes the scoping of the document object to access some internal methods to execute getElementById, which the window object doesn't have. If you must do shortcuts like that, do:
    Code:
    function x(id) {
        return document.getElementById(id);
    }

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    um... id's and names? i'm not trying to retrieve an element, i'm trying to aim a pointer at the methods.

    Code:
    var bob = new Object();
    bob.somemethod = function(a,b,c){return a + b + c;};
    
    var shortcut = bob.somemethod;
    
    alert(shortcut(1,2,3)); // outputs 6

  • #7
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    jkd - thanks for the response. that was my first guess too, but even explicitly referencing document and window, it fails - there's nothing in the error that pertains to anything but mozilla's engine.

    i've looked into the error message a bit more since, and it seems that there's no reason for it not to work - from everything i've seen it's nothing more glamorous than either a) a mistake, or b) deliberate non-compliant behavior specific to gecko. the more i get to know the moz/nn set up, the less i like it.

  • #8
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by subhailc
    a) a mistake, or b) deliberate non-compliant behavior specific to gecko. the more i get to know the moz/nn set up, the less i like it.
    There is nothing standardized about that sort of behavior.

    You could always do:

    var x = document.getElementById;
    var myElement = x.call(document, "hello");

  • #9
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    nothing standardized about pointers? not in the document / browser-centric oop model, but that's just a (limited) implementation of the language - and seems to be one that causes a great deal of aggravation to users, myself included. the object model provides no tangible advantage, and offers only alleged transparency and intuitivity in lieu of functionality - a uneven trade at best, certainly when it seems that the reality has fallen far short of the ambition.

    a method is a property of an object. you can reference any property of any object with declaration and simple assignment. for moz to supercede that in regard to certain arbitrary conditions is non-standard behavior.

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by subhailc
    nothing standardized about pointers? not in the document / browser-centric oop model, but that's just a (limited) implementation of the language - and seems to be one that causes a great deal of aggravation to users, myself included. the object model provides no tangible advantage, and offers only alleged transparency and intuitivity in lieu of functionality - a uneven trade at best, certainly when it seems that the reality has fallen far short of the ambition.

    a method is a property of an object. you can reference any property of any object with declaration and simple assignment. for moz to supercede that in regard to certain arbitrary conditions is non-standard behavior.
    Oh, but there is a good standard, JSON. Using JSON every method, variable, function, object is treated like an objects' property:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function showT(id){
    var method='getElementById'
    alert(document[method](id).innerHTML)
    }
    onload=function(){showT('mydiv')}
    </script>
    </head>
    <body>
    <div id="mydiv">text</div>
    </body>
    </html>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by subhailc
    a method is a property of an object. you can reference any property of any object with declaration and simple assignment. for moz to supercede that in regard to certain arbitrary conditions is non-standard behavior.
    Yes, and as a property of an object, it is well-justified in requiring that it is executed in the context of its object's scope. There is no distinction between methods and functions in Javascript, so it needs the scope to know what to operate on. Mozilla isn't superceding anything, working as expected. There can be multiple document objects in a scripting environment, so one would expect there to be a dynamic reference to it at some point. Besides, I gave you code to get around this problem.

    Alternatively:
    Code:
    (function(f) {
        document.__defineGetter__("getElementById", function() {
            return function(id) {
                return f.call(document, id);
            }
        });
    })(document.getElementById);
    If you must have it your way, you can always create closures around the methods and overload the properties to return those instead of the default methods.

  • #12
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    JSON looks simplier then a closure...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #13
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by Kor
    JSON looks simplier then a closure...
    But it doesn't cache the lookup. Microsoft pushed out an article recently with some DOM-tips for IE, and it mainly had to do with caching DOM objects and methods, since the DOM is implemented in a very strange way in IE (methods aren't actually Functions, objects aren't actually Objects, etc). Using the array notation just looks up the method every time, whereas storing document.getElementById keeps it ready to use at any time.

  • #14
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by jkd
    Besides, I gave you code to get around this problem.
    Quote Originally Posted by subhailc
    i'm not looking for a solution - i'd like to know why, purely for edification.

    tyia.
    the point isn't how to get around it - it's only a pointer - you get around it by writing it verbosely. don't get your panties in a bunch because i bring to light one of the (myriad) flaws in the board's pet browser.





    Quote Originally Posted by jkd
    Yes, and as a property of an object, it is well-justified in requiring that it is executed in the context of its object's scope.
    and how's that compromised by making it available to a pointer, like every/any other object?





    Quote Originally Posted by jkd
    There is no distinction between methods and functions in Javascript
    obviously, and not at all relevant. note
    Code:
    var a = String.fromCharCode;
    alert(a(100));
    var b = Math.round;
    alert(b(12.34234));
    var c = Array.prototype.sort;
    alert(c.call([1,"a","z","m",10]));
    var d = {e:{f:{g:{h:{i:function(args){return args;}}}}}};
    var j = d.e.f.h.i;
    alert(j("..."));





    Quote Originally Posted by jkd
    so it needs the scope to know what to operate on
    lol





    Quote Originally Posted by jkd
    Mozilla isn't superceding anything
    um... it's written it's interpereter to handle arbitrary conditions with arbitrary instructions. every other object instantiation obeys the rules; by allowing/forcing the document object to behave differently, you've created the problem mozilla-lemmings have accussed IE of for years (btw, IE behaves appropriately as regards this.)





    Quote Originally Posted by jkd
    working as expected
    not expected by anyone who realizes js!=dom.





    Quote Originally Posted by jkd
    There can be multiple document objects in a scripting environment, so one would expect there to be a dynamic reference to it at some point
    and there won't be multiples of any other kind of object?





    Quote Originally Posted by jkd
    If you must have it your way
    why are you being discourteous? did i hurt your feelings?





    bottom line: it doesnt need to be treated differently (e.g., IE); doing so limits rather than enriches the scripting environment, and creates implementation-specific idiosyncracies that're supposed to be the antithesis of the whole standards 'movement'.

  • #15
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by jkd
    But it doesn't cache the lookup. Microsoft pushed out an article recently with some DOM-tips for IE, and it mainly had to do with caching DOM objects and methods, since the DOM is implemented in a very strange way in IE (methods aren't actually Functions, objects aren't actually Objects, etc). Using the array notation just looks up the method every time, whereas storing document.getElementById keeps it ready to use at any time.
    Yes and no. Caching methods/elements might lead to a lack of memory, and IE is wellknown for this habbit when using closures.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •