SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Jul 30, 2007, 12:26 #1
- Join Date
- Jun 2007
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
document.images.imageName is null or not an object
When I run my code,
…
document.images.imageName.src=this.h_image();
…
I got the error message: “document.images.imageName is null or not an object”
but using the alert(this.h_image()), there is ../img/image1.jpg in a alert box.
Why can get this error?
-
Jul 30, 2007, 12:35 #2
- Join Date
- Jul 2007
- Posts
- 345
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try document.images[imageName].src
-
Jul 30, 2007, 12:49 #3
- Join Date
- Jun 2007
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have tried it, but still getting the error “document.images.imageName is null or not an object”.
-
Jul 30, 2007, 13:39 #4
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Then the variable document.images.imageName doesn't exist. Can you show the code in context?
-
Jul 30, 2007, 13:51 #5
- Join Date
- Jun 2007
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
<img src="../../img/h_030.jpg" name="imageName" Id="imageName" width="470" height="334">
I have tried to use document.images['imageName'].src=this.h_image();
and document.getElementById('imageName').src=this.h_image();
it is still to get same error.
Please help to find which is wrong.
-
Jul 30, 2007, 14:01 #6
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Where do you run the javascript code from? (Where is the <script> tag placed)
-
Jul 30, 2007, 14:52 #7
- Join Date
- Jul 2007
- Posts
- 345
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes, if your script is running before the image has loaded then your img may not yet be part of the DOM and so imageName may well be null.
To test it, you could put your code inside a function attached to window.onload:
window.onload = function() { /* your code */ };
That way, your image will have loaded before the code runs.
-
Jul 31, 2007, 08:12 #8
- Join Date
- Jun 2007
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The document.getElementById('imageName').src call path and file name of
images from constructor. Does you mean to put the constructor or method
into the window.onload = function() { put constructor or method in here? };
I'm confusing.
-
Jul 31, 2007, 13:10 #9
- Join Date
- Jun 2007
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have resolved this problem which is to set the length of Array and Array[o] is not reasonable in the object of the constructor, to cause an error: document.getElementById('imageName').src is null or not an object.
Thank you for your help and suggestion.
Bookmarks