Check if an image url is valid with javascript/jquery

    [javascript]
    function imageTest(url) {
    var imageTarget = $(‘#urlImageTester’); imageTarget.prop(‘src’, url);
    var props = [‘naturalHeight’, ‘fileCreatedDate’]; var tests = []; var answer;
    for (i in props) { tests.push(imageTarget.prop(props[i])); }
    if($.browser.msie){(tests[1] == ‘undefined’) ? answer = false : answer = true;}else{(tests[0] == 0) ? answer = false : answer = true;}
    return answer;
    }

    // create an image and hide it with css, the id used here us urlImageTester
    // the function uses jquery to check if your using IE or another browser and proforms tests dependant on the browser.
    // the function returns false if the url passed to it, is broken or invalid
    [/javascript]

    Leave a Reply