0

I have the following code:

function fn_get_natural_dim(){
    var width = this.width;
    var height = this.height;

    var ratiow = width/600;
    var ratioh = height/300;
    $("#output").append(img_name,",");

    if(ratiow>=ratioh)
        {
        height = height/ratiow;
        $(slide_image).css("width","600px");
        $(slide_image).css("height",height);
        var margin = (300-height)/2;
        $(slide_image).css("margin-top",margin);
        }
    else
        {
        width = width/ratioh;
        $(slide_image).css("width",width);
        $(slide_image).css("height","300px");
        var margin = (600-width)/2;
        $(slide_image).css("margin-left",margin);
        }
}
var max_count = $(".slider").children().length;
for(var count=1;count<=max_count;count++)
    {
    var count_string = count.toString();
    var img_name = "img" + count_string;
    var slide_image = document.getElementById(img_name);

    var img = new Image();
    img.onload = fn_get_natural_dim;
    img.src = $(slide_image).attr("src");
    }
});

For each image (i have three at the moment), im getting its natural dimensions so that i can calculate the margin necessary to fit a 600x300 div.

but i only works for the very last one, ie the third one. in other words, in the #output, it shows "img3,img3,img3," (called by $("#output").append(img_name,",");) how do i make it show "img1,img2,img3,"??

4
  • possible duplicate of function inside for loop (js/jquery) Commented Jul 24, 2013 at 12:04
  • You asked the exact same question less than an hour ago. Commented Jul 24, 2013 at 12:05
  • @Barmar it's not the exact same question - this code is (somewhat) fixed with the answer I gave to the previous question. Now the OP has a different problem. Commented Jul 24, 2013 at 12:08
  • @Alnitak Yeah, I jumped the gun a little. Commented Jul 24, 2013 at 12:11

2 Answers 2

2

You can do this:

    img.onload = (function(img_name, slide_image) {
        return function() {fn_get_natural_dim(img_name, slide_image); };
    })(img_name, slide_image);

and change fn_get_natural_dim to take the image name as a parameter:

function fn_get_natural_dim(img_name, slide_image) {

The above code captures each successive value of img_name in a closure variable.

Sign up to request clarification or add additional context in comments.

8 Comments

by "change fn_get_natural_dim to take the image name as a parameter." do you mean make it to fn_get_natural_dim(img_name) ?
Yes, that's exactly what I mean.
hmm it doesn't seem to work. first of all, for some reason, the #output shows "#img1,#img3,#img2" and the margins for #img1 and #img2 are not changed, and #img3 becomes too big for some reason
You're running this in the onload handler, and images won't necessarily finish loading in the same order that you started them. They'll be displayed in whatever order they loaded.
i see. well, i guess that's not too big of a problem. but the margins do not seem to work properly
|
-1

Edit

As Barmar pointed out, this solution isn't exactly correct. I'll leave it up as it's a fairly common mistake and is important to note the difference between his solution and my own.


Pass img_name into your fn_get_natural_dim function:

function fn_get_natural_dim( img_name ) { ... }

Then call it like:

img.onload = function() { fn_get_natural_dim(img_name); };

5 Comments

That won't work because img_name isn't a closure variable. You need a wrapper function to close it.
@Barmar: Why not? The function has access to img_name from my point of view, since it is defined inside the loop.
But the loop is not a variable scope in JS. So each iteration references the same img_name variable.
Then you can create a new scope to bind the value, if needed with a new function.
@zkar Barmar is correct. See stackoverflow.com/questions/643542/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.