Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does 'new Image()' allow for use of cache (JavaScript)

If I use new Image() to load up an image in JavaScript, will it use a cached version if possible, or will it always load up a fresh copy?

var imgObj = new Image();
imgObj.src = 'http://...';
imgObj.onload = function (loadedImg) { }
like image 269
Mr. Flibble Avatar asked Dec 04 '22 11:12

Mr. Flibble


2 Answers

One thing to note is that if you want onload to always happen (even when it's in cache) you should define onload before src.

var imgObj = new Image();
imgObj.onload = function (loadedImg) { }
imgObj.src = 'http://...';
like image 71
25 revs, 4 users 83% Avatar answered Dec 06 '22 01:12

25 revs, 4 users 83%


It'll load from cache if it's there, the same way a <img> in your markup would.

like image 33
Nick Craver Avatar answered Dec 05 '22 23:12

Nick Craver