SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alternative Image

    Hi,
    I want to acheive the following functionality using JAVASCRIPT -:
    To be able to specify alternate URL incase the original image is not found on the server.
    In HTML there is functionality for displaying alternate text.
    But I want some functionality for alternate image.
    I saw a solution wherein I could iterate over all the images in the html page and then verify whether img.completed==null. But surprisingly eventhough the image is not found , the value of completed is not null. So I felt that using the completed atrribute was not the right way.

    I either need to verify whether the image exists on the server using JAVASCRIPT or do some other thing with some other attribute which might not have come to my attention till now.

    Hope somebod will suggest a solution

    Regards,
    Sasank Y

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    img.completed is not available on all current web browsers. The only way I have found to test if an image loaded successfully is to make a copy of the image and then test the width. If it loaded the copy's width will be greater than zero. You can't test the width of the original since that will return the value set in the HTML even if it doesn't load.

    See http://javascript.about.com/library/blfailimg.htm for the code that does this.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •