You can do it by using the load() event.
When you first go to start the loading, change your image to show the preloader, or however you want to do it. Also, attach a "load" event to it.
Then, once the image is loaded, remove your preloader and show the image.
You'll need to do a similar trick for the CSS related one (the background url). What you can do is load the image directly (by creating a new Image()
), while settings the background url to your preloader icon.
Then, once the image is loaded, do the switch. Since the browser only loads an image once regardless where it is used, it should have the same effect.