0

I want to add some arguments to all images in a website, but these are dynamic. Unfortunately the (only) way I have to do this would be by intercepting the image before loading. Javascript would be the only way to go here since I can change the header. Any third party libraries like jquery are not an option.

Something like: http://www.example.com/acme.jpg

Would be captured and transformed into

http://www.example.com/acme.jpg?v=120

The way I see it javacript would have to be, inline, in the header of the page, before the images start to load. I don't need to change the html itself, only intercept when the browser calls the image and tweak it a bit as ilustrated.

Is this possible?

nsn
  • 193
  • 10
  • Think this needs clarification. Is it your page or are you loading remote pages? – Seth T Apr 28 '15 at 20:14
  • possible duplicate of [Change src of image before request has been sent](http://stackoverflow.com/questions/14415027/change-src-of-image-before-request-has-been-sent) – David Sherret Apr 28 '15 at 20:17
  • @DavidSherret It's similar but not the same. I can't change anything in the already existing html. Maybe only add some javascript on top. None of the solutions is usefull – nsn Apr 28 '15 at 20:40
  • @nsn see the first sentence in [this answer](http://stackoverflow.com/a/14415059/188246) specifically. – David Sherret Apr 28 '15 at 20:41
  • @DavidSherret I would assume that if the js is in an external file no if its in-line. Am I missing something? – nsn Apr 28 '15 at 20:43

1 Answers1

2

You could try this jquery solution :

$(function() {
  $( "img" ).load(function() { // select your images here
      var src = $( this ).attr("src");
      $(this).attr("src", src + "?v=120" ); // set the new url here
  });
}
Harijoe
  • 1,771
  • 1
  • 16
  • 27
  • That requires jquery. I dont want to rely on that. Thank you. – nsn Apr 28 '15 at 20:37
  • It's so easier with JQuery. If you wan't a pure js solution, you can just rewrite the function in my answer. The logic is the same. – Harijoe Apr 28 '15 at 20:47
  • The solution is simple, true, but I think by the time it's executed images already started to load. – nsn Apr 28 '15 at 20:52
  • That's probably true, but once the `src` has been changed, the new images should start to load. I think you won't be able to notice it, and should anyway give it a try. – Harijoe Apr 28 '15 at 21:04