Javascript ได้ให้ 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!!!