Check if link if image link

Posted on 2009-02-13
Last Modified: 2012-05-06
I have a url for product images given to me

If the image does not exist i want to display a default image but without following the image link i cant tell whether it exists, so can i do this in code

how do i detect that the image is not available in code?


Question by:david_88
    LVL 3

    Expert Comment

    Pretty sure you can't check if files exists with JavaScript.
    You'd have to do it in php, asp, jsp or similar.

    Author Comment

    oh ok thats fine, im building in php do you have any example code

    LVL 16

    Accepted Solution

    Something like this
    $filename = '/images/original.jpg';
    if (file_exists($filename)) {
    <img src='/images/original.jpg'>
    } else {
    <img src='/images/default.jpg'>

    Open in new window

    LVL 15

    Assisted Solution

    at code snippet... It works on ie6,ff3,safari,opera as well
    please rename image src for testing...
    <image src="" onerror="notAbleLoadImage();" />
    <script language="javascript">
    function notAbleLoadImage(){
    alert('not Able to Load Image do what ever you want');

    Open in new window

    LVL 15

    Expert Comment

    you could find is image.src is wrong path or unreadable by onerror event.. you could fire a javascript function by it

    Author Closing Comment

    Used the php option, but thanks both for your help

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    c# string 6 28
    Hover Gone 16 18
    javascript - pull variable in from other file 1 17
    what style is causing the padding 3 2
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now