By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Javascript slideshow issues with getElementById.

helimeef
P: 77
Hiya, I'm upgrading a this slideshow to Object Oriented code (so I can reuse it for other clients), and I'm having some problems getting the library to work. Here is what it's like so far:
Expand|Select|Wrap|Line Numbers
  1. (function(){
  2.     var img;
  3.     var imgArr;
  4.     var currSlide = 0;
  5.     function Slideshow(image,slides) {
  6.         img = document.getElementById(image);
  7.         imgArr = slides;
  8.         this.Next=function(amount) {
  9.             if(!amount) var amount=1;
  10.             if(currSlide+1<imgArr.length) {
  11.                 currSlide+=amount;
  12.             }
  13.             else {
  14.                 currSlide = 0;
  15.             }
  16.             img.src = imgArr[currSlide];
  17.             return this;
  18.         }
  19.         this.Prev = function(amount) {
  20.             if(!amount) var amount=1;
  21.             if(currSlide>0) {
  22.                 currSlide-=amount;
  23.             }
  24.             else {
  25.                 currSlide = imgArr.length-1;
  26.             }
  27.             img.src = imgArr[currSlide];
  28.             return this;
  29.         }
  30.     }
  31. })();
To create a slideshow and go to the next image (if it worked) you would do this:
Expand|Select|Wrap|Line Numbers
  1. var Show = new Slideshow('id_of_image_to_rotate',['img1.png','img2.jpg','img3.gif']);
  2. Show.Next();
However when I do this, HomeSite and FF Error Console tell me "document.getElemenyById(...) is null or not an object". I have fixed it by wrapping all of the code in window.onload, but what kind of library would require that? I figured that (function() {}); was used to remove that error, but I guess not.
Any ways I can fix this?
Oct 11 '07 #1
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027
Functions are objects, see An Introduction to Function Objects.
Oct 15 '07 #2

helimeef
P: 77
Functions are objects, see An Introduction to Function Objects.
Well, that really wasn't my problem (I think), but I got it to work anyway. I ended up doing this:
Expand|Select|Wrap|Line Numbers
  1. var Slideshow;
  2. Slideshow = (function(image_id,slides){
  3.   //the rest...
  4. });
  5. // The code that activates the slideshow
  6. window.onload = function() {
  7.   var show = new Slideshow('image_id',{'1.jpg','2.jpg'});
  8.   show.Next().Previous().Whatever();
  9. }
So I needed to put the code in window.onload. There ya go.
Oct 16 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.