This guide demonstrates how to preload images into the browser cache using jQuery, improving website performance by reducing load times. The images are added to a hidden div element within the DOM.
(function($,D,W) { var JQUERY4U = {}; JQUERY4U.UTIL = { images: { loadingImage: '<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174076317693190.gif" class="lazy" alt="jquery add image to browser cache " /></img>', ajaxImage: '<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174076317789628.gif" class="lazy" alt="jquery add image to browser cache " /></img>', savingImage: '<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174076317761887.gif" class="lazy" alt="jquery add image to browser cache " /></img>' }, preloadImages: function() { $('body').append('<div id="img-cache" style="display:none"></div>'); $.each(JQUERY4U.UTIL.images, function(i,v) { $('#img-cache').append(v); }); } } $(D).ready(function($) { JQUERY4U.UTIL.preloadImages(); }); })(jQuery,document,window);
This code creates a hidden div
with the ID "img-cache" and appends image elements to it. The $.each
loop iterates through the images
object, adding each image to the cache.
Alternative Approach:
A simpler method for preloading images without a hidden div:
// Array of image URLs var imageArray = ['image1.jpeg', 'image2.png', 'image3.gif']; // Preload images $.each(imageArray, function(index, src) { new Image().src = src; });
This approach directly creates Image
objects, assigning the src
attribute. The browser will then fetch and cache these images without needing to add them to the DOM.
This section answers common questions about adding images to a webpage using jQuery.
Use the append()
method to add an image to a div:
$('#myDiv').append('<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174076317883720.png" class="lazy" alt="jquery add image to browser cache " />');
JavaScript's createElement()
and appendChild()
methods achieve the same result:
var img = document.createElement('img'); img.src = 'image.png'; document.getElementById('myDiv').appendChild(img);
append()
Dynamically add images using variables:
var imagePath = 'image.png'; var altText = 'My Image'; $('#myDiv').append('<img src="' + imagePath + '" alt="' + altText + '">');
AJAX allows for dynamic image loading and appending:
$.ajax({ url: '/get-image-path', // Endpoint to fetch image path success: function(data) { $('#myDiv').append('<img src="' + data.imagePath + '" id="dynamicImg' + data.imageId + '" alt="Dynamic Image">'); } });
In Laravel, use the asset()
helper within the AJAX success callback:
$.ajax({ url: '/get-image', success: function(data) { $('#myDiv').append('<img src="' + "{{ asset('" + data.image + "') }}" + '" alt="Laravel Image">'); } }); ``` Remember to adjust paths and endpoints to match your specific project setup.
The above is the detailed content of jquery add image to browser cache. For more information, please follow other related articles on the PHP Chinese website!