4

I am trying to built a websocket application in IE9 but I have the following Javascript error:

IE9 Console:

SCRIPT438: Object doesn't support property or method 'map' 
websock.js, line 211 character 5

websock.js function:

function send_string(str) {
    //Util.Debug(">> send_string: " + str);
    api.send(str.split('').map(
        function (chr) { return chr.charCodeAt(0); } ) );
}

Also in IE9 console str = the text I entered. and if I try to split it first then I get the correct array of the string but still map is not working.

For example if I try to send "text":

str.split("") = ['t','e','x','t']

And I found this in the console. But unfortunately .map is not working. Any suggestions?

PS:

I tried to change the w3school code this link:

document.write(str.split("").map(
        function (chr) { return chr.charCodeAt(0); } ) + "<br />");

And map is working here with the desirable result using IE9!

glarkou
  • 7,023
  • 12
  • 68
  • 118

3 Answers3

3

IE9 supports map, but most possibly your html page is rendered in quirks mode, that's why. Try adding a doctype, and see if that solves the problem.

ustun
  • 6,941
  • 5
  • 44
  • 57
  • I think you are right because after I added `if (!('map' in Array.prototype)) { Array.prototype.map= function(mapper, that /*opt*/) { var other= new Array(this.length); for (var i= 0, n= this.length; i – glarkou Feb 14 '12 at 08:48
  • I usually add nowadays, and it takes care of it. See http://stackoverflow.com/questions/3726357/why-does-ie9-switch-to-compatibility-mode-on-my-website too. That function you added should rather stay in order to make it compatible with IE8 and below, though. IE8 and below doesn't have other functions like indexOf, that will bite you in the future too. – ustun Feb 14 '12 at 08:51
  • Also, if you open Developer Tools in IE, it will tell you in which mode it is working now. – ustun Feb 14 '12 at 09:16
  • 1
    Adding solved this problem in IE9 for me, too. – Steve Saporta Mar 28 '14 at 01:55
0

According to the ES5 compatibility table, IE9 does support Array#map. Visit http://kangax.github.com/es5-compat-table/ and look in the “This Browser” column.

Make sure the browser is in IE9 mode.

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
  • but why is working in w3schools? I edited my question. Do they somehow parse the functions and return only the output? BTW the site that you gave me shows that `Array.prototype.map` is supported by IE9 – glarkou Feb 14 '12 at 08:31
  • https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map MAP is supported by IE9! – glarkou Feb 14 '12 at 08:37
-1

FF implements map:

Array.prototype.hasOwnProperty('map') // true

IE doesn't implement map:

Array.prototype.hasOwnProperty('map') // false

Sorry, it seems you'll have to code your own map function.

sinsedrix
  • 4,336
  • 4
  • 29
  • 53
  • but why is working in w3schools? I edited my question. Do they somehow parse the functions and return only the output? – glarkou Feb 14 '12 at 08:31
  • https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map MAP is supported by IE9! – glarkou Feb 14 '12 at 08:37