জাভাস্ক্রিপ্টে স্ট্রিং এনকোড (encode) করতে escape() ফাংশন দেওয়া আছে। কিন্তু যেহেতু escape() ফাংশনটি এখন ডিপ্রিকেটেড (deprecated), তাই ভালো হবে encodeURI() বা encodeURIComponent() ব্যবহার করা।
সিনট্যাক্স-১
escape(string);
সিনট্যাক্স-২
encodeURIComponent(str);
উদাহরণ-১
নিচের উদাহরণে, escape() মেথড ব্যবহার করে স্ট্রিং "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
উদাহরণ-২
নিচের উদাহরণে, encodeURIComponent() মেথড ব্যবহার করে প্রদত্ত স্ট্রিং এনকোড করা হয়েছে এবং ফলাফল আউটপুটে দেখানো হয়েছে।
<html> <body> <script>
document.write(encodeURIComponent("Tutorix is the best e-learning platform!!!"));
</script> </body> </html>
আউটপুট
Tutorix%20is%20the%20best%20e-learning%20platform!!!