JavaScript มีฟังก์ชัน escape() สำหรับ เข้ารหัส (Encode) String แต่ escape() ถูก Deprecated แล้ว ควรใช้ encodeURI() หรือ encodeURIComponent() แทน
Syntax-1
escape(string);
Syntax-2
encodeURIComponent(str);
ตัวอย่างที่ 1
ในตัวอย่างนี้ ใช้ escape() เข้ารหัส String "Tutorix is the best e-learning platform!!!" และแสดงผลลัพธ์
<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() เข้ารหัส String และแสดงผลลัพธ์
<html> <body> <script> document.write(encodeURIComponent("Tutorix is the best e-learning platform!!!")); </script> </body> </html>
ผลลัพธ์
Tutorix%20is%20the%20best%20e-learning%20platform!!!