-1

I am not trying to load an image, but say that I want to load an image first but then call that image at a later time. Can I do this?

Image, text, ad, entire page etc. I want to target specifically what I want to load first and I do not want it to show until after all of my content if so be it.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Matthew
  • 81
  • 1
  • 8

1 Answers1

1

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.

Andreas Hagen
  • 2,335
  • 2
  • 19
  • 34
  • Sorry no. Load content before anything else. Only show it after everything has loaded. NOT LOOKING FOR AN IMAGE. Looking for a way to load any amount of content, even if an entire script. – Matthew Aug 19 '15 at 15:03
  • I'm sorry, I just skimmed through your question and did not see the nuance you requested. Have added some more explanations for different cases. =) – Andreas Hagen Aug 20 '15 at 19:21