Stefan,
> There are only a handful of other elements with similar
> shortcuts: APPLET, OBJECT, EMBED, FORM, IFRAME.
:-) There goes my idea to use the "name" approach to add a byline under the
image.
> The whole thing is almost certainly a legacy feature that had to
> be kept in the standard because many existing scripts rely on it.
That would explain why only a few elements have it.
> 1) When there is no image with the name "slide" - then
> `document.slide` is undefined and `document.slide.length`
> throws a ReferenceError.
I assumed as much, but as I needed a Zero result I thought it could not hurt
to ask it that was at all possible.
> 2) When there is exactly one such image, `document.slide` is the
> image object itself and `document.slide.length` is undefined (images
> don't have a `length` property), so the loop is not entered.
Yep, that is what I thought too (it being the object itself, not a
single-element array). But the same as the above, I thought it could not
hurt to ask.
> Both of these can be solved by using getElementsByName(),
> which always returns an array-like collection (which may or
> may not contain items):
Understood, and the best approach in the light of the above-described
problems (different results for Zero, one and more-than-one named element
situations).
> There are still issues:
>
> * The original code doesn't declare the `i` variable, creating
> an unnecessary global variable.
:-) I blame my novice status in regard to JS here. But yes, thats not
nessesary. Note to self: I need to look up the rules in regard to the
scope of variables.
> * It uses custom element attributes. This leads to validation
> errors (if the HTML is ever validated, which it should), and
> can be avoided by using `data-*` attributes instead.
Thanks for the reference, have to look into them.
> * It augments host objects (adds a `slideIndex` property to the image
> objects). While the current major browsers all support this, it has
> historically led to problems that were hard to debug. In general, it's
best
> avoided, and there are other ways to associate custom values with objects.
A guess: using the above mentioned "data-*" attributes ?
> * The original image (in the "src" attribute) is loaded, but then
> immediately switched out for slide1. This is unnecessary.
Not quite: That first image will be visible for anyone having JS blocked.
It was a consious decision to have it behave that way.
> * The original code uses `a+ ++b`, which I personally think is a
> capital offense in coding ;) What this does:
> - use ++ to put `undefined` in a numeric context, giving 0
> - use ++ to pre-increment that value, giving 1
> - use + for string concatenation, giving "slide1"
> All that in the conditional expression in an `if` statement.
Well, I also thought about that (being an offence). But as I have worked
with other languages where non-existing variables where simply
created-on-use and given the value Zero, I did not see anything strange in
JS doing the same.
As for it being part of a conditional expression, the language C{something}
does not seem to have any troubles with it, so I did/do not see a problem
here either.
> * Referring to `document.slides[i]` six times makes the code unnecessarily
> wordy.
Yes, I thought about that too. But here I gave priority of the code being
small over being absolutily easily readable.
> * <script language="JavaScript"> is quite obsolete now. Use
> <script type="text/javascript"> or just <script> instead.
Thanks for the suggestion. I assume that "script" now just defaults to JS
(as opposed to Microsofts own VBS) ?
By the way, any idea why a script type should be "*text*/javascript" ? Are
there any other types of JS available (precompiled (semy-)binary blobs
perhaps?) ? In other words, that "text" seems to be rather superfluous ...
> Concerning the popup blocking issue, consider using the
> console for debugging instead of alert(). Calling alert() in
> a loop is asking for trouble.
True. Especially when you do it with a small, or even no delay at all. :-)
I also realized that I need to check if JS has some sortof debug-output
available (writing to a/the JS debug console).
But, as I mentioned, when I removed that "alert" popup box the (not posted)
script/function only catering to a single named IMG element failed to run
properly on a certain machine, and I have no clue why (other than the
assumption that something on that computer purposely interfered with the
script for some reason). It however worked perfectly wit the posted
script, and *two* named IMG elements.
Thanks for all the info.
Regards,
Rudy Wieser
-- Origional message:
Stefan Weiss <
krewe...@gmail.com> schreef in berichtnieuws
nf3hhc$g4$1...@news.albasani.net...