javascript - Image src relative path to absolute path - Stack Overflow

I have website and now making a hybrid app for it.I get all my blog post using Jquery get method.Howe

I have website and now making a hybrid app for it. I get all my blog post using Jquery get method. However the issue is that <img src="/media/image.png"> is sometime relative url and sometime an absolute url.

Everytime an absolute url breaks the image showing 404 error.

How to write Jquery function to find if src is absolute and change it to

.png

I will not be able to provide any code samples I have tried since I am not a front end developer and tried whole day solving it.

Note: I need to change images present only in <div id="details"> div.

I have website and now making a hybrid app for it. I get all my blog post using Jquery get method. However the issue is that <img src="/media/image.png"> is sometime relative url and sometime an absolute url.

Everytime an absolute url breaks the image showing 404 error.

How to write Jquery function to find if src is absolute and change it to

https://www.example./media/image.png

I will not be able to provide any code samples I have tried since I am not a front end developer and tried whole day solving it.

Note: I need to change images present only in <div id="details"> div.

Share Improve this question asked Dec 2, 2018 at 12:14 Sapna SharmaSapna Sharma 7541 gold badge9 silver badges21 bronze badges 8
  • 1 /media/image.png should always work, it is absolute. You must have some that don't have leading / like src="media/image.png" which is relative. Can you fix the source? – charlietfl Commented Dec 2, 2018 at 12:28
  • @charlietfl If it is on same domain it work. I am working on a hybrid app so need to change to url with domain name. It works perfectly on my website but not on app since it is not on same domain. – Sapna Sharma Commented Dec 2, 2018 at 12:28
  • I am using Jquery to get post content of a blog post. It gives relative urls which works on website since it is in same domain and file system. But on app it is giving 404 error. – Sapna Sharma Commented Dec 2, 2018 at 12:30
  • So you are running this app on different domain than source of images? – charlietfl Commented Dec 2, 2018 at 12:31
  • 1 Show example of the jQuery get request. Best to fix it there before inserting into page – charlietfl Commented Dec 2, 2018 at 12:45
 |  Show 3 more ments

4 Answers 4

Reset to default 1

You should always use same path for all the images, but as of your case you can loop through images and append the domain, as of the use case I have added the domain in variable you can change it as per your requirement.

You can use mon function or image onload to rerender but I h

Note: image will rerender once its loaded.

var imageDomain = "https://homepages.cae.wisc.edu/~ece533/";

//javascript solution
// window.onload = function() {
//   var images = document.getElementsByTagName('img');
//   for (var i = 0; i < images.length; i++) {
//     if (images[i].getAttribute('src').indexOf(imageDomain) === -1) {
//       images[i].src = imageDomain + images[i].getAttribute('src');
//     }
//   }

// }

//jquery solution
var b = 'https://www.example.';
$('img[src^="/media/"]').each(function(e) {
  var c = b + $(this).attr('src');
  $(this).attr('src', c);
});

//best approach you are using get request
//assuming you are getting this respone from api
var bArray = ["https://www.example./media/image.png", "/media/image.png"]
var imgaesCorrected = bArray.map(a => {
  if (a.indexOf(b) === -1) {
    a = b+a;
  }
  return a;
});
console.log(imgaesCorrected);
img {
  width: 50px
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="/media/image.png">
<img src="https://www.example./media/image.png">

document.querySelectorAll('#details img').forEach(img => {
  const src = img.getAttribute('src');
  // use regex, indexOf, includes or whatever to determine you want to replace the src
  if (true) {
    img.setAttribute('src', 'https://www.example.' + src);
  }
});

The best would be to do this with the response html from the ajax request before inserting into the main document so as to prevent needless 404 requests made while changing the src

Without seeing how you are making your requests or what you do with the response here's a basic example using $.get()

$.get(url, function(data){
    var $data = $(data);
    $data.find('img[src^="/media/"]').attr('src', function(_,existing){
       return 'https://www.example.' + existing
    });

    $('#someContainer').append($data)'

})

You can just get all the images from an object and find/change them if they don't have absolute url.

var images = $('img');

for (var i = 0 ; i < images.length ; i++)
{
	var imgSrc = images[i].attributes[0].nodeValue;
	if (!imgSrc.match('^http'))
  {
  	imgSrc = images[i].currentSrc;
    console.info(imgSrc);
  }
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="/media/test.jpg">
<img src="/media/test.jpg">
<img src="https://www.example./media/test.jpg">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745670155a4639354.html

相关推荐

  • javascript - Image src relative path to absolute path - Stack Overflow

    I have website and now making a hybrid app for it.I get all my blog post using Jquery get method.Howe

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信