स्ट्रिंग को एन्कोड करने के लिए हमें encodeURIComponent() या encodeURI() की आवश्यकता होती है और स्ट्रिंग को डीकोड करने के लिए हमें decodeURIComponent() या decodeURI() की आवश्यकता होती है। प्रारंभ में, हमने स्ट्रिंग को एन्कोड करने के लिए escape() का उपयोग किया था लेकिन चूंकि यह डिप्रिकेटेड है, हम अब encodeURI() का उपयोग कर रहे हैं।
सिंटैक्स-1
encodeURIComponent(string);
सिंटैक्स-2
decodeURIComponent(string);
उदाहरण
निम्नलिखित उदाहरण में, प्रारंभ में एक स्ट्रिंग ली गई और encodeURI() का उपयोग करके एन्कोड किया गया और बाद में decodeURI() का उपयोग करके डीकोड किया गया। बाद में दोनों एन्कोडेड और डीकोडेड परिणाम आउटपुट में दिखाए गए।
<html> <body> <p id = "encoding"></p> <script> var str = "Tutorix is the best e-learning platform"; var enc = encodeURI(str); var dec = decodeURI(enc); var res = "After encoding: " + enc + " </br>" + "After Decoding: " + dec; document.getElementById("encoding").innerHTML = res; </script> </body> </html>
आउटपुट
After encoding: Tutorix%20is%20the%20best%20e-learning%20platform After Decoding: Tutorix is the best e-learning platform