Re: help with arrays

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Kevin Spencer wrote:

You're too kind, Trevor. In fact, I agree with you that "it's horribly
complex." There are no external JavaScripts in the page, only 2 script
blocks.

The tags you found so confusing are in fact, simply non-existant HTML
tags. I can't remember seeing them used this way before, but browsers
will ignore tags they don't recognize, so they were probably used in
the same way as comments (not at all standards-compliant, but works).

The most complex aspect of the code is the use of objects. JavaScript
is "pseudo-object-oriented" in that you can define objects in
JavaScript, but they are variant objects, and do not support
encapsulation, inheritance, and polymorphism in the way that true OOP
does. A JavaScript object is basically an aggregate of process and
data that can be changed in structure at any time. Because such
objects are variant in nature, it's pretty hard to follow the code in
such a script. It can be done, but it's a rather painstaking process.

The script also uses browser-detection, which is fairly
straightforward, and cryptic naming, which is not. On first blush, it
looks like what it's doing is using an array of images, combined with
an array of URLs, to build dynamically a changing image that, when
clicked, opens a new window with the URL of the current image loaded
in it.
Unfortunately, I just don't have the hour or so to take it apart.

Well, thanks for that info. - great stuff as always, which I need to digest.

I am glad that I wasn't alone in thinking this would be difficult to decipher

I wonder whether the OP would not be better off rewriting the whole thing using "plain" J(ava)Script, i.e., just set up an array of
images, and a button which invokes a JS function which simply uses setInterval to cycle through the images. The same button could
also be used to stop the cycling.

Here is a function which does a similar task
<script type = "text/javascript">
var ss_run, ss_lock = false
function auto()
{
if (!ss_lock)
ss_run = setInterval("chgImg(1)", 5000)
else
window.clearInterval(ss_run)
ss_lock = !ss_lock
}
</script>

It is invoked by
<input type="button" size="10" value="Start/Stop Slideshow" onclick="auto()" />

chgImg is simply a function which increments a global counter and retrieves the next image from the array into id = "slide"
<img id="slide" src="" alt="" />

The basics of it are (array example from my site):
var ss_ImgNum = 0
var Pictures = new Array(
"0000-90-nessie" ,
"01-03-16-michelles-birth-16th-march-2001" ,
"02-01-27-michelle-10mths" ,
"02-06-06-phil-and-michelle" ,
"02-11-03-melissa" ,
"04-11-22-kate-and-julie" ,
"04-01-21-6-adam-on-sprinkler" ,
"04-01-21-bryan-on-sprinkler")
function chgImg(){
document.getElementById('slide').src = "images/" + Pictures[ss_ImgNum] + ".jpg"
ss_ImgNum ++
if (ss_ImgNum >= Pictures.length)
ss_imgNum = 0
}

My function has more than this in it; I have just extracted what I think is important and made a few (untested) changes
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au


.



Relevant Pages

  • Re: Preload images
    ... Now there is only one array of images which looks ... Avoid global variables. ... Prototype.js was written by people who don't know javascript for people ...
    (comp.lang.javascript)
  • Re: Load a list of images in an Array on the server dynamically
    ... > or expand the available images for the start site. ... > files are hardcoded in a javascript and filled ... > hard coding, this means, somehow the javascript must fill an array ... When a mortal uploads a new image/Flash rebuild the List. ...
    (comp.lang.java.developer)
  • Re: Load a list of images in an Array on the server dynamically
    ... > or expand the available images for the start site. ... > files are hardcoded in a javascript and filled ... > hard coding, this means, somehow the javascript must fill an array ... When a mortal uploads a new image/Flash rebuild the List. ...
    (comp.lang.java.programmer)
  • Re: javaScript and Ajax - IE7
    ... That's positive as all of the images should be in the document. ... That's worse than using an array. ...   getTransport: function{ ... some code someone has written in javaScript which includes some ...
    (comp.lang.javascript)
  • Re: .join() !== + a + b; // for some a and b
    ... This is a situation that is probably all too common in many, if not most, programming languages, where slips in the construction of an expression result in an alternative expression, rather than a syntax error. ... There are languages where assignment would not be allowed in that context, but in javascript assignment is an expression and all expressions are allowed to drive - if - statements. ... The consequence of missing the comma out is that two expressions that would each define one element in an array become a single expression, ... The use of the comma operator in the expression in the property accessor's brackets may make the outcome more obscure but it still does not prevent the whole expression from being valid. ...
    (comp.lang.javascript)