The answer depends on what you want to load.
For images what you want to do is to add an invisible image to your page. This forces the browser to download the image. Whenever you want to display it you just make it visible again.
To make an image invisible set the display
CSS property to none
, and to make it visible again set it to block
(or any other visible value).
FYI, CSS can be edited by accessing the style.theCssProperty
of the HTMLElement
.
For content in general you want to use AJAX. Wikipedia has a working example on their page about it: https://en.wikipedia.org/wiki/Ajax_(programming)
Scripts are a bit trickier, because when they are loaded, they are executed automatically, so the only way to preload them would be to load them with AJAX, and then do an eval on the content when you need it. Do not do this however, because the sources of scripts loaded this way will be all wrong which makes it horrible to debug.
If you have access to edit the script you want to preload then wrap it in a function and load it the normal way, then whenever you need it you just call the function.