How can I change my image url to CDN URL using JavaScript or jQuery
For example urls in my webpage are
https://example.com/img/img.jpg
Then it should be replaced by
https://mycdn.com/https://example.com/img/img.jpg
How can I change my image url to CDN URL using JavaScript or jQuery
For example urls in my webpage are
https://example.com/img/img.jpg
Then it should be replaced by
https://mycdn.com/https://example.com/img/img.jpg
Consider following snippet:
$('img').each(function(){
var img = $(this);
var srcOld = img.attr('src');
var srcNew = 'https://mycdn.com/' + srcOld;
img.attr('src', srcNew); // Updating src here
});
Explanation: The above snippet loops to each
img
element and updates src of each one. Whereas this way HTML page will load all images twice first for local images and then CDN images. Better way to update src from backend.