I have a portion of code that depends on knowing the width of an image on my page.
If I fire it immediately (at the top of $(function(){
it sometimes doesn't work, as the image hasn't loaded yet.
If I use jquery (I'm already using it for other stuff on the page) to fire on load(), it NEVER fires. Presumably because the image has loaded before the JS even executes, or because it's cached.
$('#photo').load(function(){
alert('loaded');
});
//doesn't fire
If I do the following, it does work, but after a noticeable delay while it fetches the image for the second time:
$('#photo').load(function(){
alert('loaded');
});
$('#photo').attr('src',$('#photo').attr('src')+'?'+new Date().getTime());
//fires, but is slow
Surely in 2016 there must be a nice way of doing this? Either in jquery or vanilla JS.