JavaScript replace img path

Hi JavaScript guru. I have a simple JS question on how to replace the entire IMG src path on a page.

Currently, my IMG tags look like this:

<img src="path/to/image.jpg" alt="" />

Required Conclusion:

<img src="../image.jpg" alt="" />

So, when the page loads, it will go through all the IMG tags and replace the SRC path. Thank you in advance!

+3
source share
1 answer
for (var image, src, images = document.images, l=images.length, i=0; i<l; i++){
    image = images[i];
    src = image.src;
    image.src = ".." + src.substring(src.lastIndexOf("/"));
}
+5
source

Source: https://habr.com/ru/post/1727707/


All Articles