I have an array of images that I create dialogs from.
I need to know the width of the image to make that the width of the dialog.
//Used to get dimensions of image
var image = new Image();
//Set Image
image.src = ImageArray[currentImageVal + direction].src;
//Get direction to move photos
switch (direction) {
case 1:
//Set current val
currentImageVal = currentImageVal + 1;
break;
case -1:
//Set current val
currentImageVal = currentImageVal - 1;
break;
}
//Set image
$('DialogImagesBig').attr('src', ImageArray[currentImageVal].src);
//Set new current image
CurrentDialogImage = ImageArray[currentImageVal].src;
console.log(image.src);
console.log(image.width);
//Check if it is less than 450
if (image.width < 450) {
//Adjust css
$('.ui-dialog').css('width', image.width + 50);
//Edit dialog position
$('.ImageDialogDiv').dialog("option", "position", 'center');
} else {
//Normal width
$('.ui-dialog').css('width', '500');
//Edit dialog position
$('.ImageDialogDiv').dialog("option", "position", 'center');
}
The problem is that more times then not the image will not load up in time (the console.log(image.width) will equal 0 but console.log(image.src) will be correct).
Is there a way for me to pause the rest of the script until the image is loaded (everything after image.src)?
You can attach rest of the code in
img.onloadfunction. This will get called when the image gets loaded.Make sure you set the
image.srcafter this function declaration.