SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Member
    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?

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try document.images[imageName].src

  3. #3
    SitePoint Member
    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”.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    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?

  5. #5
    SitePoint Member
    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.

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    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)

  7. #7
    SitePoint Evangelist
    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.

  8. #8
    SitePoint Member
    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.

  9. #9
    SitePoint Member
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •