javascript charCodeAt - different values in JsFidd

2019-07-12 21:00发布

This line, when saved in a local html file, alerts 229:

<script>alert("基".charCodeAt(0))</script>

But when the alert is copied to JSFiddle, it provides 22522.

Why is there a difference?

1条回答
来,给爷笑一个
2楼-- · 2019-07-12 21:07

The reason is because JSFiddle charset is utf-8

You will need to set the right charset inside your meta tag inside your head tags.

With the following meta tag it will show 22522 and without it will show 229:

<meta charset="UTF-8">

Please check the following Links:

ref_html_utf8

html_charset

Tested in local file with HTML looking like this:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta charset="UTF-8">
<script>alert("基".charCodeAt(0))</script>
</head>
<body>

</body>
</html>
查看更多
登录 后发表回答