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 3 of 3

Thread: Image exists?

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

    Image exists?

    Hi. I've been trying to write Javascript code that checking if an image exists. If it doesn't exist, I want to load a known image. I've tried:

    if (document.images) {
    var tester=new Image();
    tester.onload="cImg('good')"
    tester.onerror="cImg('bad')"
    tester.src="http://www.temp.com/image.jpg"
    }

    function cImg(flg) {
    if (flg == "good") {
    document.images.main.alt = "Image is good"
    document.images.main.src = "http://www.temp.com/image.jpg"
    } else {
    document.images.main.alt = "Known image"
    document.images.main.src = "image.jpg"
    }
    }

    Problem. As far as I can see, the onload and onerror don't fire. Whether the image exists or not. At least in my IE6. What's the problem? I've tried document.images.main.onload and onerror directly, nothing. Any help is appreciated...Dennis

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Solved the problem. onclick, onload, onerror cannot be strings. Example:

    var tester=new Image()
    tester.onload=function() {cImg('good')}
    tester.onerror=function() {cImg('bad')}
    tester.src="http://www.temp.com/image.jpg"


  •  

    Posting Permissions

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