When I click the link the first time, the popup is not centered, but the second time it is. I've followed the answers of other questions that say to use 'positionTo': 'window'
, but the problem happens whether I have it or not. There are other solutions that say to use a timeout, but I don't want to use that.
function setImage()
{
$('#image-popup img').attr('src', 'https://upload.wikimedia.org/wikipedia/commons/7/7b/Orange-Whole-%26-Split.jpg');
$('#image-popup img').on('load', function() {
console.log('loaded image from click');
$('#image-popup').popup('reposition', {'positionTo': 'window'});
});
}
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<a href='#image-popup' data-rel="popup" data-position-to="window" onclick='setImage()'>Open image</a>
<div id='image-popup' data-role='popup'>
<a href="#" data-rel="back" class="ui-btn ui-corner-all ui-shadow ui-btn-a ui-icon-delete ui-btn-icon-notext ui-btn-right">Close</a>
<img class="popphoto" src="" alt="orange">
</div>
</body>
</html>
Note that you'll need to empty the cache and hard reload if you run this multiple times.