चेतावनी: escape() फंक्शन JavaScript 1.5 से अप्रचलित (deprecated) है। आधुनिक कोड में encodeURI() या encodeURIComponent() का उपयोग करें।
ऐतिहासिक उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style>
body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; padding: 20px; }
.sample { font-size: 18px; font-weight: 500; }
button { padding: 10px 20px; background: #ffc107; color: #333; border: none; border-radius: 4px; cursor: pointer; }
</style> </head> <body>
<h1>JavaScript escape() (अप्रचलित)</h1>
<div class="sample"></div>
<button class="Btn">URI एन्कोड करें (escape)</button>
<h3>ऊपर दिए गए बटन पर क्लिक करके escape() से URI एन्कोड करें</h3>
<script>
const sampleEle = document.querySelector(".sample");
const url = "https://www.google.com/images?size=420px&name=8s1f.jpg";
document.querySelector(".Btn").addEventListener("click", () => {
sampleEle.innerHTML = escape(url);
});
</script> </body> </html>
आउटपुट
https%3A//www.google.com/images%3Fsize%3D420px%26name%3D8s1f.jpg
escape() vs encodeURI() vs encodeURIComponent()
| विशेषता | escape() | encodeURI() | encodeURIComponent() |
|---|---|---|---|
| स्थिति | ❌ अप्रचलित | ✅ मानक | ✅ मानक |
| यूनिकोड सपोर्ट | ❌ खराब (केवल %xx) | ✅ UTF-8 | ✅ UTF-8 |
| एन्कोड नहीं करता | @ * _ + - . / | , / ? : @ & = + $ # | कोई नहीं |
| स्पेस एन्कोडिंग | %20 | %20 | %20 |
क्यों escape() अप्रचलित है?
- यूनिकोड वर्णों को सही ढंग से हैंडल नहीं करता (केवल ISO-8859-1)
- कुछ URI वर्णों को एन्कोड नहीं करता जो सर्वर समस्याएं पैदा कर सकते हैं
- ECMAScript मानक से हटा दिया गया (केवल अनुलग्नक B में ब्राउज़र कम्पैटिबिलिटी के लिए)
आधुनिक विकल्प
// पूरा URL
encodeURI("https://example.com/path?name=value");
// URL कंपोनेंट (क्वेरी पैरामीटर मान)
encodeURIComponent("hello world&more"); // "hello%20world%26more"
// फॉर्म डेटा के लिए
new URLSearchParams({name: "value with spaces"}).toString();