[英]string.split not working as intended in Firefox? Works fine in Chrome
having a bit of an issue here. 在这里有一个问题。 I have this code:
我有以下代码:
//phoneNumber is a string ie ('01☂916☂5234321')
var phoneNumberSplit = phoneNumber.split('☂');
console.log(phoneNumberSplit);
//in Chrome this returns as ["01", "916", "5234321"], in Firefox this returns as
//[ "01☂916☂5234321" ]
I later call phoneNumberSplit[1]
which in Chrome is fine, but in Firefox it says it's undefined
. 后来我打电话给
phoneNumberSplit[1]
,在Chrome中可以用,但是在Firefox中说undefined
。 Why does string.split
return two different things depending on which browser I'm in? 为什么
string.split
根据我所用的浏览器返回两个不同的东西? The documentation says that it works in both Firefox and Chrome. 该文档说它可以在Firefox和Chrome中使用。 Any help?
有什么帮助吗?
edit oooook I figure out what my issue was. 编辑,我知道我的问题是什么。 On the page I was testing this on the
charset="UTF-8"
was missing from the meta tag and wasn't reading the unicode character. 在页面上,我正在
charset="UTF-8"
上测试此charset="UTF-8"
,但meta标记中缺少该字符,并且没有读取unicode字符。 In Chrome I guess they have UTF-8 on by default and in Firefox they do not, or something. 在Chrome中,我猜他们默认情况下启用了UTF-8,而在Firefox中则默认情况下没有启用。 Whoops.
哎呀
I figured out what my issue was. 我弄清楚我的问题是什么。 On the page I was testing this on the
charset="UTF-8"
was missing from the meta tag and wasn't reading the unicode character. 在页面上,我正在
charset="UTF-8"
上测试此charset="UTF-8"
,但meta标记中缺少该字符,并且没有读取unicode字符。 In Chrome I guess they have UTF-8 on by default and in Firefox they do not, or something. 在Chrome中,我猜他们默认情况下启用了UTF-8,而在Firefox中则默认情况下没有启用。 Whoops.
哎呀
The meta tag is required to tell the browser the character encoding. 必须使用meta标签来告知浏览器字符编码。 Firefox 39 did give the correct result but also a warning in the console that the character encoding was incorrect.
Firefox 39确实给出了正确的结果,但在控制台中也警告了字符编码不正确。 Try this code with the meta tag included/removed to see the difference.
尝试使用包含/删除了meta标记的此代码,以查看区别。
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
<script>
//phoneNumber is a string ie ('01☂916☂5234321')
var phoneNumber='01☂916☂5234321';
var phoneNumberSplit = phoneNumber.split('☂');
console.log(phoneNumberSplit);
//in Chrome this returns as ["01", "916", "5234321"], in Firefox this returns as
//[ "01☂916☂5234321" ]
</script>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.