[英]Array.prototype.join not working in IE 8
I have used the code snippet like below in my working scenario. 我在工作场景中使用了如下代码段。 It's working fine in IE's latest versions but join function is not working in IE 8 Version.
在IE的最新版本中运行正常,但在IE 8版本中,join功能不起作用。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var str = "TEST\n";
var lines = str.split(/\r?\n/).join("\r\n");
debugger;
</script>
</body>
</html>
How to resolve this problem? 如何解决这个问题? Thanks in Advance.
提前致谢。
The problem in your particular example would rather be the split()
function, not the join()
. 在您的特定示例中,问题可能是
split()
函数,而不是join()
。 The split()
is what behaves differently between JScript 6 (IE8) and later JScript versions in that the length of the resulting array is different if the separator actually ends or begins the string. split()
是JScript 6(IE8)和更高版本的JScript之间的不同之处,如果分隔符实际上是结束还是开始于字符串,则结果数组的长度是不同的。 In this case, the <null>
item is dropped, meaning: 在这种情况下,
<null>
项将被删除,这意味着:
JScript 6:
JScript 6:
"TEST\\n".split("\\n").length equals 1“ TEST \\ n” .split(“ \\ n”)。length等于1
JScript 6+:
JScript 6+:
"TEST\\n".split("\\n").length equals 2“ TEST \\ n” .split(“ \\ n”)。length等于2
Again, in your example, this means that the join()
in IE8 is performed on an array containing just one element. 同样,在您的示例中,这意味着IE8中的
join()
在仅包含一个元素的数组上执行。
If instead you use replace()
--which seems a better option anyway-- you can overcome this problem, ie str.replace(/\\r?\\n/g, "\\r\\n")
. 如果改为使用
replace()
无论如何这似乎是一个更好的选择-您可以克服此问题,即str.replace(/\\r?\\n/g, "\\r\\n")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.