简体   繁体   English

string.split无法在Firefox中正常工作? 在Chrome中正常运行

[英]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.

相关问题 javascript String.split在Chrome中不起作用 - javascript String.split not working in chrome 页面在 Firefox 中无法正常工作,但在 chrome 中工作正常 - Page doesn't work as intended in Firefox but works fine in chrome String.split(&quot;¤&quot;); 不工作 - String.split("¤"); not working JavaScript无法在IE和Firefox中运行,在Chrome中运行良好 - JavaScript not working in IE and Firefox, works fine in Chrome HighCharts无法在Safari上运行,但在Firefox和Chrome上可以正常运行 - HighCharts not working on Safari but works fine on Firefox and Chrome Javascript Tabifier在Chrome中不起作用,但在Firefox上可以正常工作 - Javascript tabifier not working in Chrome, but works fine on Firefox 在Firefox中可以正常工作,但在chrome中不能正常工作 - Works fine in Firefox but not in chrome javascript 中的输入字段样式属性在 chrome 中工作正常,而在 Firefox 中不起作用 - Input field style attribute in javascript works fine in chrome and not working in firefox Bootstrap 表在 Chrome 上无法正常工作,但在 Firefox 上工作正常 - Bootstrap table not working correctly on Chrome but works fine on Firefox Protractor actions().mouseMove 命令不适用于 firefox 和 IE,对于 chrome 它工作正常 - Protractor actions().mouseMove command not working for firefox and IE, for chrome it works fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM