如何在JavaScript中编码字符串?
Javascript提供了escape()函数来编码 字符串。但是,由于不建议使用 escape()函数 ,因此最好使用encodeURI()或encodeURIComponent()。
语法1
escape(string);
语法2
encodeURIComponent(str);
示例1
在下面的示例中,使用 escape()方法,字符串“Tutorix是最好的电子学习平台!!!”进行编码并显示结果,如输出所示。
<html>
<body>
<script>
document.write(escape("Tutorix is the best e-learning platform!!!"));
</script>
</body>
</html>输出结果
Tutorix%20is%20the%20best%20e-learning%20platform%21%21%21
示例2
在下面的示例中,使用encodeURIComponent()方法对提供字符串进行编码,并显示结果,如输出所示。
<html>
<body>
<script>
document.write(encodeURIComponent("Tutorix is the best e-learning platform!!!"));
</script>
</body>
</html>输出结果
Tutorix%20is%20the%20best%20e-learning%20platform!!!
热门推荐
2 足球队祝福语简短
6 万寿祝福语简短
10 上班收到花束祝福语简短
11 公司28 周年祝福语简短
12 六一互换礼物祝福语简短
13 别人过大寿祝福语简短
14 虎年开新车祝福语简短
15 20岁简短生日祝福语
16 父亲祝福语大全简短语
17 18岁祝福语搞笑简短
18 怀念青春祝福语简短