473,769 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'document[...]' is null or not an object with rotating images

27 New Member
What I have is a is two sets of images,(Weather , optical) there is about 30 images of is each set.

I have script that will rotate through the images on the main page.

The user is able to view one or the other img sets with a simple onclick

This works great in Mozilla but in IE I get the error document[...]null or not an object, line 95, char 4.

I've spent hours on this, but no luck.

javascript
Expand|Select|Wrap|Line Numbers
  1. //Declare and set some variables
  2. var type;
  3. var interval = 5000;
  4. var random_display = 0;
  5. var imageArray;
  6. var imageNum = 0;
  7. var totalImages = 0;
  8.  
  9. //this function is called on page load
  10. //it sets the divs off, looks for a cookie 
  11. //turns divs on depending on the cookie
  12. function noDiv() {
  13.    //sets both divs display to off
  14.    $('weather_pic').style.display = 'none';
  15.    $('optical_pic').style.display = 'none';
  16.  
  17.    //gets the cookie 
  18.    var ImgCookie = getCookie("pic_type");
  19.  
  20.    //if cookie is set calls the switchDiv to turn the div display on
  21.    //else to defaults to the weather_pic div
  22.    if (ImgCookie) {
  23.       switchDiv(ImgCookie);
  24.    } else {
  25.       switchDiv('weather_pic');
  26.    }
  27. }
  28.  
  29. //this function turns the divs on or off and sets the cookie accordingly
  30. function switchDiv(picType) {
  31.    if (picType == 'weather_pic') {
  32.       $('optical_pic').style.display = 'none';
  33.       $('weather_pic').style.display = 'inline';
  34.       cookies(picType);
  35.       rotate(picType);
  36.    }
  37.    if (picType == 'optical_pic') {
  38.       $('weather_pic').style.display = 'none';
  39.       $('optical_pic').style.display = 'inline';
  40.       cookies(picType);
  41.       rotate(picType);
  42.    }
  43. }
  44.  
  45. //this is the set cookie function it deletes the old and sets a new for 30 days 
  46. //get, set, and deleteCookie is found in /support/cookie.js
  47. function cookies(picType) {
  48.    deleteCookie("pic_type");
  49.    setCookie("pic_type", picType, 30);
  50. }
  51. function rotate(picType) {
  52.    //var type;
  53.    //var interval = 5000;
  54.    //var random_display = 0;
  55.    //var imageArray = new Array();
  56.    //var imageNum = 0;
  57.    var i = 0;
  58.  
  59.    if (picType == 'weather_pic') {
  60.       var image_dir = "img/revised/";
  61.       type = "id_weather";
  62.       imageNum = 0;
  63.       imageArray = 0;
  64.       imageArray = new Array();
  65.  
  66.       for (i=1; i<=26; i++) {
  67.          imageArray[imageNum++] = new imageItem(image_dir + i + ".png");
  68.       }
  69.    }
  70.    if (picType == 'optical_pic') {
  71.       var image_dir = "img/Optics/";
  72.       type = "id_optical";
  73.       imageNum = 0;
  74.       imageArray = 0;
  75.       imageArray = new Array();
  76.  
  77.       for (i=1; i<=56; i++) {
  78.          imageArray[imageNum++] = new imageItem(image_dir + i + ".jpg");
  79.       }
  80.    }
  81.  
  82.    totalImages = imageArray.length;
  83.    switchImage(type);
  84. }
  85.  
  86. function imageItem(image_location) {
  87.    this.image_item = new Image();
  88.    this.image_item.src = image_location;
  89. }
  90.  
  91. function switchImage(place) {
  92.    var new_image = getNextImage();
  93.    document[place].src = new_image;
  94.    var recur_call = "switchImage('"+place+"')";
  95.    timerID = setTimeout(recur_call, interval);
  96. }
  97. function getNextImage() {
  98.    if (random_display) {
  99.       imageNum = randNum(0, totalImages-1);
  100.    } else {
  101.       imageNum = (imageNum+1) % totalImages;
  102.    }
  103.  
  104.    var new_image = get_ImageItemLocation(imageArray[imageNum]);
  105.    return(new_image);
  106. }
  107.  
  108. function randNum(x, y) {
  109.    var range = y - x + 1;
  110.    return Math.floor(Math.random() * range) + x;
  111. }
  112.  
  113. function get_ImageItemLocation(imageObj) {
  114.    return(imageObj.image_item.src)
  115. }
  116.  
noDiv() is called from the page load and the java works from there.
This problem is in my switchDiv() function, but I haven't been able to solve it.

here is the html

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  3. <head>
  4. <script type="text/javascript" src="support/cookie.js"></script>
  5. <script type="text/javascript" src="support/main.js"></script>
  6. </head>
  7. <body onload="noDiv()">
  8.             <div id="navtrail2">
  9.                <span onclick="switchDiv('weather_pic')"> Weather Pictures</span> :
  10.                <span onclick="switchDiv('optical_pic')"> Optical Pictures</span>
  11.             </div>
  12.                <!-- Start right Column box -->
  13.                <div id="righcolbox">
  14.                   <div id="weather_pic">
  15.                      <img height="385px" width="385px" align="right" border="3" id="id_weather" src="img/revised/1.png" alt="Weather Images" />
  16.                   </div>
  17.                   <div id="optical_pic">
  18.                      <img height="385px" width="385px" align="right" border="3" id="id_optical" src="img/Optics/1.jpg" alt="Optical Images" />
  19.                   </div>
  20.  
  21.                <!-- End right Column box -->
  22.                </div>
  23. </body>
  24. </html>
  25.  
could someone help with this?
Aug 21 '07 #1
11 2153
pbmods
5,821 Recognized Expert Expert
Heya, BD.

It looks like this is the line that is causing you grief (in switchImage()):
Expand|Select|Wrap|Line Numbers
  1. document[place].src = new_image;
  2.  
Try this:
Expand|Select|Wrap|Line Numbers
  1. var img = document.getElementsByName(place)[0];
  2. if(img)
  3. {
  4.     img.src = new_image;
  5. }
  6. else
  7. {
  8.     // Debug
  9.     alert(place);
  10. }
  11.  
Aug 21 '07 #2
bdbeames
27 New Member
I think you are on to something, but I am still having trouble

this is what I changed the function to

Expand|Select|Wrap|Line Numbers
  1. function switchImage(place) {
  2.    var new_image = getNextImage();
  3.    var img = document.getElementByName(place)[0];
  4.    if (img) {
  5.       img.src = new_image;
  6.    } else {
  7.       //debug
  8.       alert(place);
  9.    }
  10.    //document[place].src = new_image;
  11.    var recur_call = "switchImage('"+place+"')";
  12.    timerID = setTimeout(recur_call, interval);
  13. }
  14.  
IE error
Object doesn't support this property or method
line 95 char4

what am i doing wrong?

It no longer works in Mozilla as it did before I receive the error
document.getEle mentByName is not a function.
Aug 21 '07 #3
bdbeames
27 New Member
Ok fixed my one problem it is getElementsByNa me not getElementByNam e

I'm not receiving an error in IE but my images never appear. In Mozilla I just receive the error message every 5 seconds.

Does anyone know how to fix this.

Thanks
Aug 21 '07 #4
jx2
228 New Member
well im not an expert on this so i might be wrong
but this lines look weird to me

Expand|Select|Wrap|Line Numbers
  1. // those quotation marks look weird to me
  2. // iam not sure about the purpose of it... 
  3. //wouldnt you have to use eval(recure_call) after that?
  4. // pbmods probably knows 
  5.    var recur_call = "switchImage('"+place+"')";
  6.  
  7.    timerID = setTimeout(recur_call, interval);
  8.  
  9. //anyway i would try to write them :
  10.  
  11.    var recur_call = place;
  12.    timerID = setTimeout(switchImage(recur_call), interval);
  13. }
  14.  
i hope that helped
jx2
Aug 21 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, JX2.

In this code:
Expand|Select|Wrap|Line Numbers
  1.  var recur_call = "switchImage('"+place+"')";
  2.  
recur_call might, for example, evaluate to:
switchImage('id _weather');

However, you can also do this:
Expand|Select|Wrap|Line Numbers
  1. timerID = setTimeout(function() { switchImage(place); }, interval);
  2.  
Now that I'm looking at it, you actually want to do this instead:
Expand|Select|Wrap|Line Numbers
  1. var img = document.getElementById(place);
  2. if(img)
  3. {
  4.     img.src = new_image;
  5. }
  6. else
  7. {
  8.     // Debug
  9.     alert(place);
  10. }
  11.  
The document[place] syntax confused me because long-time IE programmers often get element names and IDs confused (since IE makes them more or less equivalent; this is incidentally why a lot of IE designers abuse IDs).
Aug 21 '07 #6
jx2
228 New Member
Heya, JX2.

In this code:
Expand|Select|Wrap|Line Numbers
  1.  var recur_call = "switchImage('"+place+"')";
  2.  
recur_call might, for example, evaluate to:
switchImage('id _weather');

However, you can also do this:
Expand|Select|Wrap|Line Numbers
  1. timerID = setTimeout(function() { switchImage(place); }, interval);
  2.  
thx a lot pbmods - nice trick

- i learn a lot from you!!
hough!!
Aug 21 '07 #7
pbmods
5,821 Recognized Expert Expert
We aim to please.
Aug 21 '07 #8
bdbeames
27 New Member
Ok I believe I got it. Thanks again for all the help.

I had one div/id working and couldn't get the other one to work.

After a long step by step walk through the problem was that one of img sets couldn't be displayed for some reason. They were .png images. I made them .jpg images and it worked. I don't understand why .png doesn't work but that was the problem.

Do either of you know why?

Anyways here is all my code if you ever have to do the same thing.

check out the site:
http://beta.climate.us urf.usu.usu.edu

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  3. <head>
  4.    <script type="text/javascript" src="support/cookie.js"></script>
  5.    <script type="text/javascript" src="support/main.js"></script>
  6. </head>
  7.  
  8. <body onload="noDiv()">
  9. <div id="navtrail2">
  10.    <span onclick="switchDiv('weather_pic')"> Weather Pictures</span> :
  11.    <span onclick="switchDiv('optical_pic')"> Optical Pictures</span>
  12. </div>
  13. <!-- Start right Column box -->
  14. <div id="righcolbox">
  15.    <div id="weather_pic">
  16.       <img height="385px" width="385px" align="right" border="3" 
  17. id="id_weather" src="img/revised/1.png" alt="Weather Images" />
  18.    </div>
  19.    <div id="optical_pic">
  20.       <img height="385px" width="385px" align="right" border="3" id="id_optical" src="img/Optics/1.jpg" alt="Optical Images" />
  21.    </div>
  22. <!-- End right Column box -->
  23. </div>
  24.  
  25. </body>
  26. </html>
  27.  
Expand|Select|Wrap|Line Numbers
  1. //Declare and set some variables
  2. var type;
  3. var interval = 5000;
  4. var random_display = 0;
  5. var imageArray;
  6. var imageNum = 0;
  7. var totalImages = 0;
  8. var image_dir;
  9.  
  10. //this function is called on page load
  11. //it sets the divs off, looks for a cookie 
  12. //turns divs on depending on the cookie
  13. function noDiv() {
  14.    //sets both divs display to off
  15.    $('weather_pic').style.display = 'none';
  16.    $('optical_pic').style.display = 'none';
  17.    //document.getElementById('weather_pic').style.display = 'none';
  18.    //document.getElementById('optical_pic').style.display = 'none';
  19.  
  20.  
  21.    //gets the cookie 
  22.    var ImgCookie = getCookie("pic_type");
  23.  
  24.    //if cookie is set calls the switchDiv to turn the div display on
  25.    //else to defaults to the weather_pic div
  26.    if (ImgCookie) {
  27.       switchDiv(ImgCookie);
  28.    } else {
  29.       switchDiv('weather_pic');
  30.    }
  31. }
  32.  
  33. //this function turns the divs on or off and sets the cookie accordingly
  34. function switchDiv(picType) {
  35.    if (picType == 'weather_pic') {
  36.       $('weather_pic').style.display = 'inline';
  37.       $('optical_pic').style.display = 'none';
  38.       //alert(picType);
  39.       cookies(picType);
  40.       rotate(picType);
  41.    }
  42.    if (picType == 'optical_pic') {
  43.       $('optical_pic').style.display = 'inline';
  44.       $('weather_pic').style.display = 'none';
  45.       //alert(picType);
  46.       cookies(picType);
  47.       rotate(picType);
  48.    }
  49. }
  50.  
  51. //this is the set cookie function it deletes the old and sets a new for 30 days 
  52. //get, set, and deleteCookie is found in /support/cookie.js
  53. function cookies(picType) {
  54.    deleteCookie("pic_type");
  55.    setCookie("pic_type", picType, 30);
  56. }
  57. function rotate(picType) {
  58.    var i = 0;
  59.  
  60.    if (picType == 'weather_pic') {
  61.       image_dir = "img/Optic/";
  62.       type = "id_weather";
  63.       imageNum = 0;
  64.       imageArray = 0;
  65.       imageArray = new Array();
  66.  
  67.       for (i=11; i<=20; i++) {
  68.          imageArray[imageNum++] = new imageItem(image_dir + i + ".jpg");
  69.       }
  70.    }
  71.    if (picType == 'optical_pic') {
  72.       image_dir = "img/Optics/";
  73.       type = "id_optical";
  74.       imageNum = 0;
  75.       imageArray = 0;
  76.       imageArray = new Array();
  77.  
  78.       for (i=1; i<=10; i++) {
  79.          imageArray[imageNum++] = new imageItem(image_dir + i + ".jpg");
  80.       }
  81.    }
  82.  
  83.    totalImages = imageArray.length;
  84.    switchImage(type);
  85. }
  86.  
  87. function imageItem(image_location) {
  88.    this.image_item = new Image();
  89.    this.image_item.src = image_location;
  90. }
  91.  
  92. function switchImage(place) {
  93.    var new_image = getNextImage();
  94.    var img = document.getElementById(place);
  95.    if (img) {
  96.       img.src = new_image;
  97.    } else {
  98.       //debug
  99.       alert(place);
  100.    }
  101.    //document[place].src = new_image;
  102.    var recur_call = "switchImage('"+place+"')";
  103.    timerID = setTimeout(recur_call, interval);
  104. }
  105. function getNextImage() {
  106.    if (random_display) {
  107.       imageNum = randNum(0, totalImages-1);
  108.    } else {
  109.       imageNum = (imageNum+1) % totalImages;
  110.    }
  111.  
  112.    var new_image = get_ImageItemLocation(imageArray[imageNum]);
  113.    return(new_image);
  114. }
  115.  
  116. function randNum(x, y) {
  117.    var range = y - x + 1;
  118.    return Math.floor(Math.random() * range) + x;
  119. }
  120.  
  121. function get_ImageItemLocation(imageObj) {
  122.    return(imageObj.image_item.src)
  123. }
  124.  
thanks again
Aug 21 '07 #9
pbmods
5,821 Recognized Expert Expert
Heya, BD.

Just a guess, but maybe this had something to do with it...
Expand|Select|Wrap|Line Numbers
  1.  imageArray[imageNum++] = new imageItem(image_dir + i + "-->.jpg<--");
  2.  
:P

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 21 '07 #10

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

Similar topics

1
10113
by: Winfried Koenig | last post by:
Hi everyone, I have a main page: -------------------------------------------------- <html><head><title>Test</title> </head><body> <img id="img_a" name="img_a" src="image_1.png" alt=""><br>
14
14185
by: Chris | last post by:
Heres my problem: <a href="javascript:void(document.buysell.submit())" target="_parent" onMouseOver="MM_swapImage('members','','images/membersf2.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="images/members.gif" alt="Back to members page" name="members" width="270" height="25" border="0"></a> I get the error "document.buysell" is null or not an object, but my form name is buysell and when using the submit button, which is not
2
1952
by: Aaron | last post by:
Hi, I've seen javascript code where a constructor function is passed an argument "document", and inside the function itself the assignment "this.document = document;" is made. This is the code (or the part necessary for the example): function ToggleButton(document) { ToggleButton.images = new Array(4); for(i=0;i<4;i++) { ToggleButton.images = new
5
2102
by: John | last post by:
I am rotating images at one location of my web site. My problem is if I set the width and height of the new image before I show the new image, the old image is stretched first to the new image dimensions, and if I show the new image before setting its dimensions, the new image is stretched first to the old image dimension before it is adjusted to its own dimension. I would like to load a new image with its own dimension at the same time....
1
3230
by: Sandy Bremmer | last post by:
I have seen many Javascripts that rotate images with each load or refresh of the page but so far all I've found require hard coding the image filename into the script. Does anyone know of a script that pulls images randomly from a directory? Thus I would need only add (or delete) images from that directory and they would display on the web page respectively. All the images are the same size (90x90) but there are hundreds so hard coding...
136
9447
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
4
1975
by: bdbeames | last post by:
Could someone help with this? I currently have a rotating picture that I have on our website's main page. This week we decided to add another set of pictures for users to view. The users can switch between the two different picture sets by a simple onclick where I change the picture sets. I started to modify my existing code and ran into the problem where the img within a div is not loading in IE, it works fine in Mozilla but in IE I...
10
3769
by: AC | last post by:
I had a page that does some event setup on window.onload: function prepEvents() { document.getElementById("menumap_sales").onmouseover = swapMenuSales; // etc } window.onload = prepEvents;
5
4013
by: ankit1999 | last post by:
I have a problem, everytime i'm run this page http://click2travel.in/index.php i get the this error,,,
0
9583
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5297
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.