-2
 <script>
const images = document.querySelectorAll('.view-content img')
images.forEach(item => {
  const src = item.getAttribute('data-original');
  const newSrc = 'https://example.com' + src;
  item.setAttribute('src', newSrc);
})
</script>

This code changes

<img src="/data/img/img_01.png"> to

<img src="https://example.com/data/img/img_01.png">

as you can see. but this code doesn't work in Internet Explorer 11. How can i make this code work in Internet Explorer?

stackstack
  • 15
  • 3

1 Answers1

2

Neither the arrow functions nor the forEach() method are compatible in IE11. You need to rewrite the above using ES5 like this:

<script>
    const images = document.querySelectorAll('.view-content img');

    for(var i = 0; i < images.length; i++) {
        let src = images[i].getAttribute('data-original');
        let newSrc = 'https://example.com' + src;
        images[i].setAttribute('src', newSrc);
    }
</script>

Or if you prefer using ES6 to write your JavaScript, you can use a toolchain like BabelJS to compile your JavaScript ES6 to JavaScript ES5 on production.

AndrewL64
  • 15,794
  • 8
  • 47
  • 79