0

I have made a user control, and placed some code in the $(document).ready() method, but when I place breakpoints, by the boxWidth value, and step through, I can visibly see that the html has not yet loaded, and that is why I am getting undefined values. I need the html to load first, so that I can get the width and height of an img element to calculate aspect rations etc.

A screen shot is given below:

enter image description here

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Lee
  • 8,354
  • 14
  • 55
  • 90

2 Answers2

5

You simply haven't declared those variables anywhere. Try this:

var elem = $("#Body_Body_ucImageUploadAndCrop_CropImage"),
    boxWidth = elem.width(),
    boxHeight = elem.height();

elem.Jcrop({
    onSelect: storeCoords,
    boxWidth: boxWidth,
    boxHeight: boxHeight,
    aspectRatio: boxWidth / boxHeight
});

Now that your code runs error free, you might wanna access the image only after it's loaded:

$(window).on( "load", function() {
    //Code to run after window has loaded
});

Should do it.

Esailija
  • 138,174
  • 23
  • 272
  • 326
-1

You shall be looking for resources loaded event

$.load("image_id", function(){ ... });
Samba
  • 607
  • 4
  • 19