Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript replace img path

Tags:

javascript

Hello JavaScript guru's. I have a simple JS question on how to replace all IMG src path's on a page.

Currently, my IMG tags look like:

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

Output desired:

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

So, when a page is loaded, it will loop through all the IMG tags and replace the SRC path. Thanks in advance for the assistance!

like image 299
Drew Avatar asked Feb 01 '26 10:02

Drew


1 Answers

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("/"));
}
like image 190
Roland Bouman Avatar answered Feb 03 '26 23:02

Roland Bouman