Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
getElementsByClassName: BUG in JS-engine?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mattias Campe  
View profile  
 More options Nov 18 2012, 5:59 am
Newsgroups: comp.lang.javascript
From: Mattias Campe <mattiaspuntca...@geeeemeil.com>
Date: Sun, 18 Nov 2012 11:47:49 +0100
Local: Sun, Nov 18 2012 5:47 am
Subject: getElementsByClassName: BUG in JS-engine?
Dear comp.lang.javascript

I have strange behavior when using getElementsByClassName and I don't
understand what I'm doing wrong. There are some paragraphs, where I want
to change layout by selecting the className, to "link" another className
to it. But getElementsByClassName only gets some of those classnames,
but not all. It seems such a simple exercise to make it hard to have
errors, but still I succeed in doing so :-/.

Would somebody happen to know what the problem is? You can find my code
at
http://php.olvgroeninge.be/~sac.mcampe/werkmap/getElementsByClassName...

Tested with browser Firefox 16.0.2 op Ubuntu 12.10

Kind regards
Mattias


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Honnen  
View profile  
 More options Nov 18 2012, 6:09 am
Newsgroups: comp.lang.javascript
From: Martin Honnen <mahotr...@yahoo.de>
Date: Sun, 18 Nov 2012 12:09:55 +0100
Local: Sun, Nov 18 2012 6:09 am
Subject: Re: getElementsByClassName: BUG in JS-engine?

Mattias Campe wrote:
> Dear comp.lang.javascript

> I have strange behavior when using getElementsByClassName and I don't
> understand what I'm doing wrong. There are some paragraphs, where I want
> to change layout by selecting the className, to "link" another className
> to it. But getElementsByClassName only gets some of those classnames,
> but not all. It seems such a simple exercise to make it hard to have
> errors, but still I succeed in doing so :-/.

> Would somebody happen to know what the problem is? You can find my code
> at
> http://php.olvgroeninge.be/~sac.mcampe/werkmap/getElementsByClassName...

> Tested with browser Firefox 16.0.2 op Ubuntu 12.10

Most DOM collections are "live" collections so it is difficult iterating
over them while manipulating them. Your loop manipulates the class of
elements and that way the collection changes while the loop is executed
and that leads to elements being skipped as after setting
   alleOpvallendeAlineas[0].className
that element is removed from the collection and the one that was at
index 1 is now at index 0. And the next loop iteration changes
   alleOpvallendeAlineas[1]
which is the third element in the original collection.

One way to avoid that problem is working from the end of the collection e.g.
   for (var l = alleOpvallendeAlineas.length, i = l - 1; i >= 0; i--) {
     alleOpvallendeAlineas[i].className = ...;
   }

--

        Martin Honnen --- MVP Data Platform Development
        http://msmvps.com/blogs/martin_honnen/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mattias Campe  
View profile  
 More options Nov 18 2012, 8:56 am
Newsgroups: comp.lang.javascript
From: Mattias Campe <mattiaspuntca...@geeeemeil.com>
Date: Sun, 18 Nov 2012 14:56:39 +0100
Local: Sun, Nov 18 2012 8:56 am
Subject: Re: getElementsByClassName: BUG in JS-engine?
Op 18-11-12 12:09, Martin Honnen schreef:

> Most DOM collections are "live" collections so it is difficult iterating
> over them while manipulating them. Your loop manipulates the class of
> elements and that way the collection changes while the loop is executed
> and that leads to elements being skipped as after setting
>    alleOpvallendeAlineas[0].className
> that element is removed from the collection and the one that was at
> index 1 is now at index 0. And the next loop iteration changes
>    alleOpvallendeAlineas[1]
> which is the third element in the original collection.

> One way to avoid that problem is working from the end of the collection
> e.g.
>    for (var l = alleOpvallendeAlineas.length, i = l - 1; i >= 0; i--) {
>      alleOpvallendeAlineas[i].className = ...;
>    }

I wouldn't have found that explanation myself, but it all makes sense,
once you know that :). So it's not a bug in the JS-engine after all ;).

Thank you very much for your fast and clear explanation!!

Kind regards
Mattias


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas 'PointedEars' Lahn  
View profile  
 More options Nov 18 2012, 12:43 pm
Newsgroups: comp.lang.javascript
Followup-To: comp.lang.javascript
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Sun, 18 Nov 2012 18:43:53 +0100
Local: Sun, Nov 18 2012 12:43 pm
Subject: Re: getElementsByClassName: BUG in JS-engine?

It could not have been a bug in "*the* JS-engine" because

1. There is no such thing.  Netscape/Mozilla JavaScript (e.g. in
   Mozilla-based browsers, NES compatibles, B2G, and Firefox OS) is but one
   of several ECMAScript implementations.  Other major implementations
   include JScript (e.g. in MSHTML/IE, with Windows Script Host, and on
   IIS), V8 (e.g. in Chromium/Google Chrome, on Android, and as base of
   node.js), Apple JavaScriptCore (e.g. in Safari, and on the iPhone/iPad),
   Opera ECMAScript (in Opera and Opera Mobile), and KDE JavaScript (e.g.
   in Konqueror):

   <http://PointedEars.de/es-matrix> (to be updated)

   Despite the historic name of the newsgroup, all of those implementations
   are on-topic here and in similar newsgroups in other Usenet hierarchies.

2. The feature you are using here is not part of any ECMAScript
   implementation to begin with, but of a language-independent DOM API.
   You are merely using an implementation of that with ECMAScript
   implementations:

   <https://developer.mozilla.org/en/docs/DOM>

   Scripting a DOM using an ECMAScript implementation is on-topic here as
   well.

That said, next time you should be slow(er) to declare something a bug
without being sure, much less use the word "bug" that way in the message'
Subject:

<http://www.catb.org/~esr/faqs/smart-questions.html>,
in particular
<http://www.catb.org/~esr/faqs/smart-questions.html#idp29846432>.

HTH

PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300d...@news.demon.co.uk> (2004)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »