Encoding a string in JavaScript.

Javascript Encode Method In Javascript

Decoding in Javascript can be achieved using decodeURI function. It takes encodeURIComponent (url) string so it can decode these characters. 2. unescape () function: This function takes a string as a single parameter and uses it to decode that string encoded by the escape () function.

encodeURI( complete_uri_string ) https://www.google.com/search?q=geeks%20for%20geeks encodeURIComponent( uri_string_component ) geeks%20for%20geeks escape( string ) https://www.google.com/search?q=geeks%20for%20geeks https%3A//www.google.com/search%3Fq%3Dgeeks%20for%20geeks decodeURI( complete_encoded_uri_string ) https://www.google.com/search?q=geeks for geeks decodeURIComponent( encoded_uri_string_component ) geeks for geeks unescape(string) https://www.google.com/search?q=geeks%20for%20geeks https%3A//www.google.com/search%3Fq%3Dgeeks%20for%20geeks https://www.google.com/search?q=geeks for geeks https://www.google.com/search?q=geeks for geeks

Encode URL in JavaScript?

Stick with encodeURIComponent().The function encodeURI() does not bother to encode many characters that have semantic importance in URLs (e.g. «#», «?», and «&»). escape() is deprecated, and does not bother to encode «+» characters, which will be interpreted as encoded spaces on the server (and, as pointed out by others here, does not properly URL …

var myUrl = "http://example.com/index.html?param=1&anotherParam=2"; var myOtherUrl = "http://example.com/index.html?url=" + myUrl; var myOtherUrl = "http://example.com/index.html?url=" + encodeURIComponent(myUrl); const value = encodeURIComponent(value).replace('%20','+'); const url = 'http://example.com?lang=en&key=' + value const escapedValue = encodeURIComponent(value).replace('%20','+'); const escapedFolder = encodeURIComponent('My Folder'); // no replace const url = `http://example.com/$/?myKey=$`; qs.stringify(); // gets a=1%3D2&b=Test+1 $.param() // gets a=1%3D2&b=Test+1 const queryParams = < param1: 'value1', param2: 'value2' >const queryString = new URLSearchParams(queryParams).toString() // 'param1=value1¶m2=value2' const myUrl = "http://example.com/index.html?param=1&anotherParam=2"; const myOtherUrl = new URL("http://example.com/index.html"); myOtherUrl.search = new URLSearchParams(); console.log(myOtherUrl.toString()); var myOtherUrl = "http://example.com/index.html?url=" + encodeURIComponent(myUrl); function urlencode(str) < str = (str + '').toString(); // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following. return encodeURIComponent(str) .replace('!', '%21') .replace('\'', '%27') .replace('(', '%28') .replace(')', '%29') .replace('*', '%2A') .replace('%20', '+'); >encodeURI() encodeURIComponent() const baseURL = 'http://example.com/index.html'; const myUrl = new URL(baseURL); myUrl.searchParams.append('param', '1'); myUrl.searchParams.append('anotherParam', '2'); const myOtherUrl = new URL(baseURL); myOtherUrl.searchParams.append('url', myUrl.href); console.log(myUrl.href); // Outputs: http://example.com/index.html?param=1&anotherParam=2 console.log(myOtherUrl.href); // Outputs: http://example.com/index.html?url=http%3A%2F%2Fexample.com%2Findex.html%3Fparam%3D1%26anotherParam%3D2 console.log(myOtherUrl.searchParams.get('url')); // Outputs: http://example.com/index.html?param=1&anotherParam=2 const params = new URLSearchParams(myOtherUrl.search); console.log(params.get('url')); // Outputs: http://example.com/index.html?param=1&anotherParam=2 function fixedEncodeURIComponent(str) < return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A"); >encodeURI("abc%20xyz 123") // wrong: "abc%2520xyz%20123" encodeURI(decodeURI("abc%20xyz 123")) // correct: "abc%20xyz%20123" console.log(encodeURIComponent('?notEncoded=&+')); // for a whole URI don't use encodeURIComponent it will transform // the / characters and the URL won't fucntion properly console.log(encodeURIComponent("http://www.random.com/specials&char.html")); // instead use encodeURI for whole URL's console.log(encodeURI("http://www.random.com/specials&char.html")); function fixedEncodeURIComponent(str) < return encodeURIComponent(str).replace(/[!'()*]/g, function(c) < return '%' + c.charCodeAt(0).toString(16); >); > Response.Headers["land"] = "login"; $(function () < var $document = $(document); $document.ajaxSuccess(function (e, response, request) < var land = response.getResponseHeader('land'); var redrUrl = '/login?ReturnUrl=' + encodeURIComponent(window.location); if(land) < if (land.toString() === 'login') < window.location = redrUrl; >> >); >); var url = $(location).attr('href'); //get current url //OR var url = 'folder/index.html?param=#23dd&noob=yes'; //or specify one var encodedUrl = encodeURIComponent(url); console.log(encodedUrl); //outputs folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes for more info go http://www.sitepoint.com/jquery-decode-url-string    textarea // encode string to base64 function encode() < var txt = document.getElementById("txt1").value; var result = btoa(txt); document.getElementById("txt2").value = result; >// decode base64 back to original string function decode()