I have a framework (vue.js) inserting some images on a page change (with a router, not a real page refresh). When loaded directly, I can make the page display a loading screen:
loading = true;
$(window).on( 'load', function(){
loading = false;
});
However, the $(window).on('load')
doesn't trigger if the page has been navigated via the framework. This (I assume) is because the window
is already loaded, and the loading of the new images isn't tied to the window anymore. So, the loading = false
never triggers because the window already loaded.
Here's an extremely simplified example, but it illustrates the same point:
//Loading the initial image, it works fine because it runs on window load.
console.log('Loading first image...')
$(window).on('load',function(){
console.log('First image loaded');
});
$('button').on('click',function(){
$('div').append('<img src="https://placekitten.com/200/300">');
console.log('Loading extra image...');
//This never triggers because the window is already loaded.
//I need something to trigger while the appended images are loading
$(window).on('load',function(){
console.log('Extra image loaded.');
});
});
HTML:
<img src="https://placekitten.com/200/300">
<button>Click to load extra</button>
<div></div>
Here's a codepen.