Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a string which needs to be split by three underscore characters. An example of the string might be:

var stringItemsPlanner = "Hello this___is a string___which___needs splitting into___an array";

So I use the Split() function. Fine in everything but IE8 (and probably 7 too but not tried) which gives an "Object doesn't support this property or method" error if the string doesn't contain those characters. So I found another post which said to check that the underscore characters appear in the string before splitting, so I do this:

if (stringItemsPlanner.indexOf('___') == -1){
    arrItemsPlanner = [];
}else{
    arrItemsPlanner = stringItemsPlanner.split('___');
}

But now this errors too because apparently IE8 doesn't support 'indexOf'.

After a lot of searching I've tried adding some code to the top of my script to act as a 'polyfil' for this method:

if (!Array.prototype.indexOf){
  Array.prototype.indexOf = function(elt /*, from*/){
    var len = this.length >>> 0;
    var from = Number(arguments[1]) || 0;
    from = (from < 0)? Math.ceil(from) : Math.floor(from);
    if (from < 0){
      from += len;
      for (; from < len; from++){
        if (from in this && this[from] === elt){
          return from;
        }
        return -1;
      };
    }
  }
}

But still no joy.

I'm now past the point of frustration and can't really think of any other way to get this simple thing to work.

Can anyone shed any light on this or think of an alternative way to safely split a string to an array in a way that works cross-browser? It's got to be simple but I just can't think straight now.

Thanks all!

share|improve this question
    
Are you completely sure split doesn't work in IE8? (I just tried it in that "IE8 mode" in IE9, also in "IE7 mode", and it seems fine) –  cambraca Jan 27 '12 at 20:18
    
split() should be supported in IE8 –  kinakuta Jan 27 '12 at 20:18
    
Sorry, I've made it clearer now. Split is supported by IE8 but raises an exception if the characters aren't in the string being split - which can happen in my case and is what's causing the issue. –  Dan Jan 27 '12 at 20:26
1  
Split just returns -1 if the string does not contain the needle you are looking for. I do not think it should throw an exception. –  mplungjan Jan 27 '12 at 20:28
1  
A string is a string and not an array. Array.prototype.indexOf meens that you can use [].indexOf(). String.prototype.indexOf is ECMAScript 1.0 - IE 8 definitly supports it- –  Saxoier Jan 27 '12 at 20:29

1 Answer 1

up vote 0 down vote accepted

Have a look here

http://jsfiddle.net/mplungjan/Bnx6m/

var stringItemsPlanner = "Hello this___is a string___which___needs splitting into___an array";

var arrItemsPlanner = (stringItemsPlanner.length==0 || stringItemsPlanner.indexOf('___') == -1)? []:stringItemsPlanner.split('___');

alert(arrItemsPlanner.join('\n'))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.