Is this common?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Genisio

    Is this common?

    Hi all,

    I came across some code on a big-name site, that I dont like, but need
    to support (I am writing a DOM model for a product).

    Here is the code:

    var d=document.writ eln;
    d("<HTMLSTUFF>. ....");

    Now, this works in IE, but does not work in Netscape. Netscape will
    throw an exception. The authors only did this in IE (using browser
    detection), but it is just plain bad, in my opinion.

    The variable d is a function object. It is not necesarially associated
    with document. In fact, in the Mozilla implementaiton of javascript,
    this object would be executed from the global (window) object.

    My question, is: "Is this a common thing to do?" Do people do this
    often? In what other contexts do they do this?

    I am in no way advocating this type of code... nor do I plan to use it.
    I need to support it, which is whay I ask.

    Just so anyone is curious, a MUCH better solution would be:

    function d(text) { document.writel n(text) }
    d("<HTMLSTUFF>. ....");

    Ok, Thanks,
    Brian

  • Yann-Erwan Perio

    #2
    Re: Is this common?

    Brian Genisio wrote:
    [color=blue]
    > var d=document.writ eln;
    > d("<HTMLSTUFF>. ....");[/color]
    [color=blue]
    > Now, this works in IE, but does not work in Netscape. Netscape will
    > throw an exception. The authors only did this in IE (using browser
    > detection), but it is just plain bad, in my opinion.[/color]

    Not only is testing only in IE quite awkward, but doing some browser
    detection as well is a definitively bad approach.
    [color=blue]
    > The variable d is a function object. It is not necesarially associated
    > with document. In fact, in the Mozilla implementaiton of javascript,
    > this object would be executed from the global (window) object.[/color]

    That's probably correct, but where did you see that?
    [color=blue]
    > My question, is: "Is this a common thing to do?" Do people do this
    > often? In what other contexts do they do this?[/color]

    This depends on whether the object is a host objet or a js object.

    Assuming it's a host object, then the answer is a definite NO, for the
    reasons you've pointed out above. Hosts' objects implementation isn't
    specified anywhere and differ effectively.

    Assuming it's a js object, then the answer would be NO most of the time,
    but not always. In js, you can indeed set the "this" value, using
    Function.protot ype.call or Function.protot ype.apply, so having a
    function without a context isn't a real problem, you can set a different
    context each time you call the function - it depends on which problem
    you're trying to solve.

    Gecko's hosts object seem to be designed (and can be used) like js
    objects, with a prototype and a "this" value. This means you can use the
    generic js methods on them, like Array.prototype .join on any HTML
    collection. But that also means you cannot use IE-like shortcut for the
    function (thie "this" value being required).
    [color=blue]
    > Just so anyone is curious, a MUCH better solution would be:
    >
    > function d(text) { document.writel n(text) }
    > d("<HTMLSTUFF>. ....");[/color]

    Which is what I would have suggested.


    Regards,
    Yep.

    Comment

    • Jim Ley

      #3
      Re: Is this common?

      On Fri, 05 Mar 2004 12:37:32 -0500, Brian Genisio
      <BrianGenisio@y ahoo.com> wrote:
      [color=blue]
      >Here is the code:
      >
      >var d=document.writ eln;
      >d("<HTMLSTUFF> .....");
      >
      >Now, this works in IE, but does not work in Netscape.[/color]

      It's a speed optimisation, worth a reasonable amount in IE.
      [color=blue]
      >Just so anyone is curious, a MUCH better solution would be:
      >
      >function d(text) { document.writel n(text) }
      >d("<HTMLSTUFF> .....");[/color]

      No, that would be a bad solution, since it just adds extra indirection
      slowing things down even further.

      Jim.
      --
      comp.lang.javas cript FAQ - http://jibbering.com/faq/

      Comment

      • Brian Genisio

        #4
        Re: Is this common?

        Jim Ley wrote:
        [color=blue]
        > On Fri, 05 Mar 2004 12:37:32 -0500, Brian Genisio
        > <BrianGenisio@y ahoo.com> wrote:
        >
        >[color=green]
        >>Here is the code:
        >>
        >>var d=document.writ eln;
        >>d("<HTMLSTUFF >.....");
        >>
        >>Now, this works in IE, but does not work in Netscape.[/color]
        >
        >
        > It's a speed optimisation, worth a reasonable amount in IE.
        >
        >[color=green]
        >>Just so anyone is curious, a MUCH better solution would be:
        >>
        >>function d(text) { document.writel n(text) }
        >>d("<HTMLSTUFF >.....");[/color]
        >
        >
        > No, that would be a bad solution, since it just adds extra indirection
        > slowing things down even further.
        >
        > Jim.[/color]

        I disagree. The first option is only availble in IE as far as I know.
        At least, it is not available in Mozilla. There is no way of reliably
        detecting if a browser will support this optimization method. The
        second option will work in all browsers that support document.writel n.

        Portablity is much more important than speed, in a massively viewed site.

        Brian

        Comment

        • Yann-Erwan Perio

          #5
          Re: Is this common?

          Jim Ley wrote:
          [color=blue]
          > It's a speed optimisation, worth a reasonable amount in IE.[/color]

          That makes sense, from an implementation point of view; still, the
          amusing following test gives surprising results (the shorthand function
          is *four* times slower than a regular identifier resolution):


          <div id="bar"></div>
          <script type="text/javascript">
          window.onload=f unction(){
          var d=document, gebi=d.getEleme ntById, foo;
          var d1, d2, d3;

          d1=new Date();
          for(var ii=0; ii<10000; ii++) {
          foo=d.getElemen tById("bar");
          }
          d2=new Date();
          for(var j=0; j<10000; j++) {
          foo=gebi("bar") ;
          }
          d3=new Date();

          alert((d2-d1)+"\n"+(d3-d2));
          }
          </script>

          gives 791 for the first method, and 3405 for the second one.
          [color=blue]
          > No, that would be a bad solution, since it just adds extra indirection
          > slowing things down even further.[/color]

          To me it's not a matter of speed, but if speed matters then it'd be
          better to buffer the output and write it only when required...


          Regards,
          Yep.

          Comment

          • Jim Ley

            #6
            Re: Is this common?

            On Fri, 05 Mar 2004 14:11:14 -0500, Brian Genisio
            <BrianGenisio@y ahoo.com> wrote:
            [color=blue]
            >Jim Ley wrote:[color=green][color=darkred]
            >>>function d(text) { document.writel n(text) }
            >>>d("<HTMLSTUF F>.....");[/color]
            >>
            >>
            >> No, that would be a bad solution, since it just adds extra indirection
            >> slowing things down even further.
            >>
            >> Jim.[/color]
            >
            >I disagree. The first option is only availble in IE as far as I know.
            >At least, it is not available in Mozilla. There is no way of reliably
            >detecting if a browser will support this optimization method.[/color]

            Sorry, I seem to put that badly, my point was not doing it at all and
            just using document.writel n would be appropriate, not using the
            caching method of IE.

            Jim.
            --
            comp.lang.javas cript FAQ - http://jibbering.com/faq/

            Comment

            Working...