tags:

views:

129

answers:

1

I am not getting the value of the variable of "img_srt" in load function, can any body helps me?

$(document).ready(function() {
    Get_var();
})

var img_srt='<div>address</div>';   

function Get_var() {
    $("<img />").attr("src","http://www.google.com/intl/en_ALL/images/logo.gif")
        .load(function() {
            if (this.height > 0) {
                img_srt += "<div><img src='http://www.google.com/intl/en_ALL/images/logo.gif'/&gt;&lt;/div&gt;";   
            }
        })
    alert(img_srt);
}
+5  A: 

The function which sets img_srt is called asynchronously. That is, it may be executed before or after your alert statement. Likely after, as you're having this problem.

Try something like this:

$('<img/>')
    .attr('src', 'http://www.google.com/intl/en_ALL/images/logo.gif')
    .load(function() {
        var imageLoaded = this.height > 0;

        if(imageLoaded) {
            $(this).appendTo('#myDiv');
        } else {
            $('<p/>').text('Unable to load image').appendTo('#myDiv');
        }
    });
strager
but i need that img_srt out side that load function,can you give me any help?
Srikanth
@john, Why? What are you trying to do?
strager
i need to append that to another string, which contains some other html tags.
Srikanth
Mr. Strager already img_srt contains some tags, how can i append that <img> to that img_srt?
Srikanth
@john, What are you trying to do ultimately? That is, what is your final goal?
strager
A image is dynamically loading in to my HTML page,if it is exist no problem with that.But if that image is not there "no-image" like image is coming, so i want to remove that <img> from HTML if that image is non-exist.
Srikanth
sorry for my English.
Srikanth