¿Por qué este javascript no funciona en IE pero sí en otros navegadores?

Yet another reason why I hate IE for being different. I've got this code:

var friends = document.getElementById('friendsEmail').value;
...
 var emailArray=new Array();
 emailArray=String(friends).split(','); //error here

This works in every other browser. In fact I only added the Array declaration and the explicit String conversion to get this to work in IE. However, I'm still getting this error

Webpage detalles del error

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0E; .NET CLR 1.1.4322) Timestamp: Wed, 1 Feb 2012 22:24:15 UTC

Message: Object doesn't support this property or method Line: 59 Char: 4 Code: 0

What does IE not like about this? Am I doing something obviously stupid that I'm not seeing?

EDITAR

This is what I did originally that resulted in IE errors:

var emailArray=friends.split(',');

This also gives me errors in IE

EDIT 2

I'm sorry guys. Turns out that the server was caching an older copy of the .js file and the error line I was looking at was incorrect... I apologize for wasting your time with this. The error turned out to be on a completely different line. For those who are curious it was actually failing because of this line

emailArray[count]=emailArray[count].trim();

I've already resolved that issue though.

preguntado el 01 de febrero de 12 a las 22:02

So perhaps you can remove the "why I hate IE" phrase since it had nothing to to with IE? -

@RobG I could...but it was still IE being different that caused the problem. Just a different function. -

5 Respuestas

split() creates its own array, so leave out emailArray=new Array();

Tu puedes simplemente hacer

emailArray = document.getElementById('friendsEmail').value.split(",")

Respondido 02 Feb 12, 02:02

I get the same error without the Array. I added the Array declaration after I noticed errors in IE. - rondel

That's true, but has nothing to do with the OP's question. - Wayne

The constructors for primitive types are usually not recommended. You already have a string in your friends variable, so just split y eso

Cambiar

emailArray = String(friends).split(','); //error here

a

emailArray = friends.split(','); //should work

Respondido 02 Feb 12, 02:02

I did this originally and this gave me errors as well. I only added in the Array and String to see if that was the issue. - rondel

Object doesn't support this property or method:In IE8 and IE7 - rondel

I updated my question. It turns out that this wasn't actually failing. I was looking at the wrong error line. - rondel

Split on IE is not standard. I use this bibliotecas for cross-browser splitting.

Respondido 02 Feb 12, 02:02

I did see this library and considered it. I wanted to try fixing the javascript first but I may just use this in the end. - rondel

Steven Levithan split() replacement is good and I'd recommend it, but for this particular case is unnecessary. IE is fine when splitting on a string rather than a regex. - Tim Down

I see no issue with your code. I think that you may have a problem with the value you are retrieving such that friends isn't what you think it is. You can see in this jsFiddle that the general concept you're using works in IE:

var friends = document.getElementById('friendsEmail').value;
var emailArray = friends.split(','); 

Puedes verlo funcionar aquí: http://jsfiddle.net/jfriend00/s9S99/.

So, I think the issue is retrieving your value. That must not be what you think it is (perhaps it is null sometimes).

Respondido 02 Feb 12, 03:02

I strongly suspect that the actual issue is that document.getElementById('friendsEmail').value is undefined. Are you actually trying to get the text content of the element? If so, then this works:

var friends = document.getElementById("friendsEmail");
var emailArray = friends.innerText.split(",");

Note that a simple test case on a string literal -- like "str,str1,str2".split(",") -- would have quickly demonstrated that the split was not the issue. It's always best to reduce things to the smallest testable unit. Otherwise, you're just guessing about the source of the error.

Demostración: http://jsfiddle.net/JHZJe/

Respondido 02 Feb 12, 03:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.