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
)?