Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Names of Images

  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Names of Images

    Hello everyone.

    I want to be able to access the names of images which could be variably defined. That is, I won't know going in how many there will be or which ones. I have the following code:

    <html>
    <body>
    This is an image reference test
    <br>
    <img src="gifimage.gif" name=im1>
    <img src="jpgimage.jpg" name=im2>
    <script language="javascript">
    document.write(document.images["im1"])
    document.write(document.images[1])
    </script>
    </body>
    </html>

    All I'm trying to do is display the name of each image on the page, but what I get is [object][object] instead.

    Is there something I'm doing syntactically wrong here?

    I was hoping to see "gifimage.gif" and "jpgimage.jpg".

    Thanks in advance.

    Russell.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    FL, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you could do this

    --------

    <html>
    <body>
    This is an image reference test
    <br>
    <img src="gifimage.gif" name=im1>
    <img src="jpgimage.jpg" name=im2>
    <script language="javascript">
    document.write(document.im1.src)
    document.write("<BR>")
    document.write(document.im2.src)
    </script>
    </body>
    </html>

    ---------

    Hope this helps
    CYWebmaster.com - See why we dot com!!
    ACJavascripts.com - Cut & Paste Javascripts!
    SimplyProgram.com - Personal Blog

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Names of Images

    Originally posted by rah111
    All I'm trying to do is display the name of each image on the page, but what I get is [object][object] instead.
    That is correct behaviour of javascript, you just print out the references to some document.images objects.

    Is there something I'm doing syntactically wrong here?

    I was hoping to see "gifimage.gif" and "jpgimage.jpg".
    Not quite "wrong", you just missed something. See, document.images is a collection of objects. Each of these objects has also properties. Compare that to a car e.g.. The car is the object, and a property of it would be color, price, maximum speed etc. Ok so far? Now imagine that the car has also a name. If you want to get that name, you'd ask like

    "what is the name or the second car".

    In javascript, we could code that like

    document.images[1].name

    In your example, if you do

    document.write(document.images[1].name);

    it would print "im2". Why? Because "name" is defined by the attribute "name" in the <img> tag. You rather wanted to get it's filename, or speaking more technically, the value of it's src attribute, so you have to retrieve

    document.write(document.images[1].src);

    See how the individual attributes of a tag correspond to properties of an images object?
    Putting it all together, since document.images is an object that behaves like an array, you can print out the "filename" of each image by using this snippet:

    for (var i = 0; i < document.images.length; i++) {
    document.write(document.images[i].src + "<br />");
    }

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's the script:

    <img src="do.gif" name="img1">

    <script language="JavaScript">
    function showImgName(imgID) // The main function
    {
    document.write(document[imgID].src.split("/")[document[imgID].src.split("/").length-1]);
    }
    showImgName("img1"); // Call the function wherever you want
    </script>


  • #5
    New Coder
    Join Date
    Jun 2002
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    Thanks ACJavascript and Mordred.

    Now I get it.

    Particularly like the loop. That's just what I wanted to understand.

    Russell.


  •  

    Posting Permissions

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