Reddy
Reddy

Reputation: 1497

jQuery - Image path replace folder error

I have client specific images in many .html pages and I want to redirect the images path to other folder from default folder.

I am trying to achieve the same by following code from external javascript file which is working as expected. But getting an error and HTML source still showing as old file path

I am getting an error as below though Images are loading

GET file:///E:/User/Images/test.png net::ERR_FILE_NOT_FOUND

PS: I am doing this scr

$(document).ready(function () {
  $(function () {
    var img = $("img");
    img.each(function () {
      var src = $(this).attr('src');
      if (src.indexOf('Images/') > -1) { // Default path of the images
        $(this).attr('src', src.replace("Images", "Images/client")); // New path
      }
    });
  });
});

And also, if I check source code, still it is showing as Images/test.png instead of Images/client/test.png

Upvotes: 0

Views: 398

Answers (2)

Ray A
Ray A

Reputation: 1351

First, you have to use a web server instead of what you are doing,

GET file:///E:/User/Images/test.png net::ERR_FILE_NOT_FOUND

means you are open the file locally.

However, your code is correct and doesn't have any error, you are trying to replace the word (client) with (Images/client) and because your src doesn't contain any word (client), the replace is not taking a place, your correct code should be something like:

$(this).attr('src', src.replace("Images", "Images/client"));

This will work for sure, test and let us know if it worked.

Thanks.

Upvotes: 1

Dimag Kharab
Dimag Kharab

Reputation: 4519

It should be ,

$(this).attr('src', src.replace("Images", "Images/client"));

Upvotes: 1

Related Questions