how to hide every <div> ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Winter

    #16
    Re: how to hide every &lt;div&gt; ?

    On Thu, 12 Aug 2004 09:30:52 +0100, john henry bonham <with@he.ld> wrote:
    [color=blue]
    > Richard Cornford wrote:
    >[color=green]
    >> john henry bonham wrote:
    >> <snip>
    >>[color=darkred]
    >>> for (var i = 0 ;i < document.getEle mentsByTagName{ 'div' ).length;
    >>> i++ ); {
    >>> document.getEle mentsByTagName{ 'div' )[ i ].style.visibili ty =
    >>> 'hidden';
    >>> }[/color]
    >> <snip>
    >> Apart form the absence of verification for the browser's supportfor the
    >> required features (which can be assumed to be in codepreviously
    >> executed but not shown), This is a ridiculouslyine fficient formulation.[/color]
    >
    > Sorry you lost me. Can you explain that in layman's terms?[/color]

    Just what he said.

    Every time you call document.getEle mentsByTagName( ), a supporting browser
    must search the document for all such tags and build an array containing
    them. To make matters worse, it must do this twice for each iteration:
    once to get the length property, and again to get a reference to the i'th
    object.

    Note that I said "supporting browser", above. As you point out yourself,
    this method is only supported by relatively recent browsers. There are
    many people that use browsers that will not support this method, so this
    script will do nothing more than cause an error.

    To sort out the latter problem, use feature testing:

    if(document.get ElementsByTagNa me) {
    // method supported

    This should also be applied when accessing the style property.

    For the former, call the method once, save the results, and use them in
    the loop:

    function setVisibility(e , v) {
    if(e) {
    if(e.style) {
    e.style.visibil ity = v;
    } else {
    e.visibility = v;
    }
    }
    }

    function hideDivs() {
    if(document.get ElementsByTagNa me) {
    var c = document.getEle mentsByTagName( 'DIV');

    for(var i = 0, n = c.length; i < n; ++i) {
    var e = c[i];

    setVisibility(e , 'hidden');
    }
    }
    }
    [color=blue]
    > Also, you have spent alot of effort in this thread pointing outerrors
    > and standars which is all well and good. What's yoursolution to the
    > problem? Or pray, tell us which solution offered isthe best.[/color]

    Well, the above could be modified to use alternatives to
    getElementsByTa gName():

    if(document.get ElementsByTagNa me) {
    var getRefsByName = function(n) {
    return document.getEle mentsByTagName( n);
    };
    } else if(document.all && document.all.ta gs) {
    var getRefsByName = function(n) {
    return document.all.ta gs(n) || [];
    };
    /*
    * Add any other alternatives here.
    *
    */
    } else {
    var getRefsByName = function() {
    return [];
    };
    }

    function setVisibility(e , v) {
    if(e) {
    if(e.style) {
    e.style.visibil ity = v;
    } else {
    e.visibility = v;
    }
    }
    }

    function hideDivs() {
    var c = getRefsByName(' DIV');

    if(c) {
    for(var i = 0, n = c.length; i < n; ++i) {
    var e = c[i];

    setVisibility(e , 'hidden');
    }
    }
    }

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail

    Comment

    • Richard Cornford

      #17
      Re: how to hide every &lt;div&gt; ?

      john henry bonham wrote:[color=blue]
      > Richard Cornford wrote:[color=green]
      >> john henry bonham wrote:[color=darkred]
      >>>for (var i = 0 ;i < document.getEle mentsByTagName{ 'div' ).length ;
      >>>i++ ); {
      >>> document.getEle mentsByTagName{ 'div' )[ i ].style.visibili ty =
      >>>'hidden';
      >>>}[/color]
      >> <snip>
      >> Apart form the absence of verification for the browser's support for
      >> the required features (which can be assumed to be in code previously
      >> executed but not shown), This is a ridiculously inefficient
      >> formulation.[/color]
      >
      > Sorry you lost me. Can you explain that in layman's terms?[/color]

      Explain what exactly? I have explained the inefficiency, and the
      requirement for feature detection is explained in the FAQ. But in
      layman's terms I probably couldn't explain either as the majority of
      laymen have no concepts or terminology that would applicable to computer
      programming and tend to find the subject extremely tedious.
      [color=blue]
      > Also, you have spent alot of effort in this thread pointing out errors
      > and standars which is all well and good. What's your solution to the
      > problem? Or pray, tell us which solution offered is the best.[/color]

      The question, as asked, is too superficial to make an assessment of what
      an optimum response should be, at least in terms of the code that should
      be applied to the problem.

      Your proposal (and a number of others) has concentrated on taking the
      words "hide every <div>" literally, regardless of the fact that hiding
      every DIV in a document is extremely unlikely to be the real
      requirement, or a good idea.

      Grant Wagner's response, in providing a direct illustration of feature
      detection applied in the context of the question, is probably going to
      prove most valuable to the OP.

      Thomas Lahn's interjection that Grant's implementation is less than
      optimally efficient as it repeats tests that will have the same outcome
      each time they are executed as they had on the first execution, is true,
      but the applicability of that notion to the OP's context remains
      unknown. Given the little that we know of the OP, I think that presentin
      g a number of interdependent, feature detecting functions (even if
      optimally efficient) as a direct substitution for the OP's single
      original might have represented moving too far up the learning curve for
      the OP to go in a single step. Even if it does represent a point the OP
      must pass through at some time in order to consider themselves a
      competent Javascript author.

      Richard.


      Comment

      • Richard Cornford

        #18
        Re: how to hide every &lt;div&gt; ?

        Thomas 'PointedEars' Lahn wrote:[color=blue]
        > Richard Cornford wrote:[color=green]
        >> Thomas 'PointedEars' Lahn wrote:
        >> <snip>[color=darkred]
        >>> The point is that defining a function/method once removes
        >>> the necessity for checking for DOM features every time it
        >>> is called.[/color]
        >>
        >> Any point in the post of yours that I was replying to is irrelevant
        >> to the observation that its implementation should be expected to
        >> fail in an ECMA 262 (3rd edition) conforming script engine.[/color]
        >
        > You may want to re-read the thread to notify the correction I made
        > because of your comments.[/color]

        I have no idea what you are going on about. While erroneous
        implementation might tend to undermine the credibility of an individual
        trying to make a point, any points made have validity, or not,
        independent of example code intended as illustration.

        Richard.


        Comment

        • Thomas 'PointedEars' Lahn

          #19
          Re: how to hide every &lt;div&gt; ?

          Richard Cornford wrote:
          [color=blue]
          > Thomas 'PointedEars' Lahn wrote:[color=green]
          >> Richard Cornford wrote:[color=darkred]
          >>> Thomas 'PointedEars' Lahn wrote:
          >>> <snip>
          >>>> The point is that defining a function/method once removes
          >>>> the necessity for checking for DOM features every time it
          >>>> is called.
          >>>
          >>> Any point in the post of yours that I was replying to is irrelevant
          >>> to the observation that its implementation should be expected to
          >>> fail in an ECMA 262 (3rd edition) conforming script engine.[/color]
          >>
          >> You may want to re-read the thread to notify the correction I made
          >> because of your comments.[/color]
          >
          > I have no idea what you are going on about. [...][/color]

          Is it as hot weather with you as with me? The correction I made
          because of your comments which you snipped above is compliant to
          ECMAScript 3, using FunctionExpress ions!


          PointedEars

          Comment

          • Richard Cornford

            #20
            Re: how to hide every &lt;div&gt; ?

            Thomas 'PointedEars' Lahn wrote:[color=blue]
            > Richard Cornford wrote:[color=green]
            >> Thomas 'PointedEars' Lahn wrote:[/color][/color]
            <snip>[color=blue][color=green][color=darkred]
            >>> You may want to re-read the thread to notify the
            >>> correction I made because of your comments.[/color]
            >>
            >> I have no idea what you are going on about. [...][/color]
            >
            > Is it as hot weather with you as with me?[/color]

            Why, would it have made more sense in cooler weather?
            [color=blue]
            > The correction I made because of your comments
            > which you snipped above is compliant to
            > ECMAScript 3, using FunctionExpress ions![/color]

            Did anyone say that it wasn't? Indeed it is almost exactly the same as
            one of the formulations used in the examples of feature detection that
            appears in the FAQ notes.

            Richard.


            Comment

            Working...