1

I'm trying to add "sip:" before a list of emails

This is my code :

// Adds sip: to skype emails to use Skype Entreprise
var elems = document.getElementsByClassName('skypemail');

Array.from(elems).forEach(function(v) {
    var elemVal = v.getAttribute('href');
    v.setAttribute('href', 'sip:' + elemVal);
});

To my links :

<a id="skypemail" href="username@website.com">test</a>
<a id="skypemail" href="username1@website.com">test</a>
<a id="skypemail" href="username2@website.com">test</a>
<a id="skypemail" href="username@website.com">test</a>

It works fine with all navigators but it is not working with IE 11

When i used the debugbar i got :

the object doesn't support this property or method "from"
usethe23
  • 155
  • 6
  • 5
    The error tells you the issue. IE doesn't support `Array.from()` https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/from – Shilly Mar 07 '17 at 16:16
  • Thank you @Shilly, do you know what should i use instead to make it work ? – usethe23 Mar 07 '17 at 16:22
  • 1
    @usethe23 How about [the polyfill from the linked page](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Polyfill)? – Bergi Mar 07 '17 at 16:32
  • @Bergi Thank you, this is what i also found and it worked ! Thank you for your help – usethe23 Mar 07 '17 at 16:47
  • @Bergi Now i'm interested as well. Are there advantages in using the polyfill over the slice method apart from being able to write `[].from()`, and hence make the code more appropriate to future standards? Update: I found IE8 doesn't like `[].slice.call()` – Shilly Mar 07 '17 at 16:51

1 Answers1

2

You can use the old slice trick to get an array from an arraylike object, like the live nodeList you have:

var elems = document.getElementsByClassName('skypemail');
var elemsAry = Array.prototype.slice.call(elems);

Array.slice always creates a new array, and by calling it on each element inside an arraylike, the elements automatically get 'mapped' (not the correct terminology, just a comparison) to the same 'index' of the real array.

Shilly
  • 8,511
  • 1
  • 18
  • 24
  • Thank you for your help ! i've found http://stackoverflow.com/questions/36810940/array-from-on-the-internet-explorer how to make it work – usethe23 Mar 07 '17 at 16:46