5

I am using the split function in JavaScript. It works fine in Firefox and Chrome, but IE displays an error when I call the split function. Is there a way to use other function like split?

6
  • 6
    Need to see your code and what is the error you're getting? Commented Jun 21, 2011 at 8:26
  • 5
    It does support, it probably fails on something else in your code. Post your code and we'll see. Commented Jun 21, 2011 at 8:26
  • We will reply as fast as you can paste your code. Please select your code after paste and click the button with {} Commented Jun 21, 2011 at 8:28
  • what version of IE are you testing with? (this is important, as there are bugs with split() in older versions of IE) Commented Jun 21, 2011 at 8:36
  • @mplungjan - in some older versions of IE, if you split a string and it has separator characters immediately adjacent to each other, it won't return the empty element. eg "x||y".split('|') gives an array with only two results "x" and "y" rather than three. I believe it was IE6 that did this; I don't have a copy of IE6 any more to verify this, but I remember it causing me huge problems a few years ago until I found out about the bug. Commented Jun 21, 2011 at 9:07

3 Answers 3

7

split Method

It's fully supported by IE8

split method for JScript 5.6

It's also fully supported by IE6

Live example using .split(/\s+/)

Tested in IE9 standards, IE9 IE8 mode, IE9 IE7 mode and IE9 quirks mode. All work.

Edit:

Turns out your actual problem is using .textContent. This does not work in IE. There are two alternatives.

Feature detection:

var str;
if (el.textContent) {
  str = el.textContent;
} else {
  str = el.innerText;
}

.nodeValue:

var str = el.nodeValue;

Sign up to request clarification or add additional context in comments.

2 Comments

i understand my problem i use that code textContent.split(/\s+/); so textcontent working in ff and crome but not working in ie . solution is use innertext which is not working in ff so is there any other solution
@VbPatel Yes .textContent is not supported. Use .nodeValue
1

When you split a number instead of String, Javascript throws - Object doesn't support this property.

Make sure you have a string value in var.

Comments

0

There is at least one difference between IE below #9 and most other browsers when dealing with string splitting-

var s='In some browsers, you can "split" a string on a parenthized delimeter and return the "split-off" bits in the array.';

s.split(/( ?['"] ?)/).join('\n')

/***************************************************/
Firefox 4.0.1>>
 In some browsers, you can
 "
split
" 
a  string on a parenthized delimeter and return the
 "
split-off
" 
bits in the array.
/***************************************************/    
MSIE 8.0>>
 In some browsers, you can
split
a  string on a parenthized delimeter and return the
split-off
bits in the array.
/***************************************************/    
MSIE 9.0>>
 In some browsers, you can
 "
split
" 
a  string on a parenthized delimeter and return the
 "
split-off
" 
bits in the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.