Re: Problem with Javascript's String::split method in IE
Scott Sauyet <[email protected]>
| Newsgroups | gmane.comp.web.dom.wdf |
|---|---|
| Message-ID | <[email protected]> |
Scott Sauyet wrote:
> I'm having a problem with splitting a string into lines in Javascript.
> [ ... see http://scott.sauyet.com/Javascript/Issues/LineSplit/ ]
David Gale posted a response that pointed me in the right direction. The
innerHTML property is changing the source text. When I replaced it with
childNodes[0].data, I cleaned up the really bizzare behavior. I still
needed to add another several lines to clear up the fact that IE doesn't
return the empty tokens in the String:split method, but that was
relatively straightforward now that this other is working. The new code
is at:
http://scott.sauyet.com/Javascript/Issues/LineSplit/3.html
and it looks like this:
window.onload = function() {
var element = document.getElementById("test")
// var text= element.innerHTML;
var text= element.childNodes[0].data;
text = text.replace(/(\r\n|\r|\n)(\r\n|\r|\n)/g, "$1 $2");
var lines = text.split(/\r\n|\r|\n/);
var s = "";
for (var i = 0; i < lines.length; i++) {
if (lines[i] == " ") lines[i] = "";
s += (i + 1) + ": " + lines[i] + "\n";
}
alert(s);
};
This is not an industrial-strength solution. I need to do several bits
of cleaning up. (There may be more than one text node, for instance,
and I really should do something slightly more sophisticated so that I'm
not replacing lines containing only a single space with blank lines.)
But it works enough to let me proceed.
Thanks for the help, David!
-- Scott
Unsubscribe
[email protected]
List info
http://www.quirksmode.org/dom/list.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/wdf-dom/
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/