2860

How do you safely encode a URL using JavaScript such that it can be put into a GET string?

var myUrl = "http://example.com/index.html?param=1&anotherParam=2";
var myOtherUrl = "http://example.com/index.html?url=" + myUrl;

I assume that you need to encode the myUrl variable on that second line?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 30
    Try looking into [encodeURI()](http://www.w3schools.com/jsref/jsref_encodeURI.asp) and [decodeURI()](http://www.w3schools.com/jsref/jsref_decodeURI.asp). – Zack The Human Dec 02 '08 at 02:39
  • 1
    You can use this tool here: http://phillihp.com/toolz/url-encode-decode/ – phillihp Sep 18 '12 at 02:13
  • 1
    See *[JavaScript urlencode function](http://phpjs.org/functions/urlencode:573)*. – Yanni Jun 30 '11 at 16:40
  • 4
    encodeURIComponent() – Andrew Mar 02 '18 at 18:52
  • Please go see [this answer](https://stackoverflow.com/a/58879100/5882233) as it is the only one using modern javascript features (supported in anything but Internet Explorer). – iron9 Jun 02 '21 at 09:44

22 Answers22

3175

Check out the built-in function encodeURIComponent(str) and encodeURI(str).
In your case, this should work:

var myOtherUrl = 
       "http://example.com/index.html?url=" + encodeURIComponent(myUrl);
user229044
  • 232,980
  • 40
  • 330
  • 338
Buu
  • 49,745
  • 5
  • 67
  • 85
  • 13
    How about adding the explanation @cms gave? `escape` is also a valid option. – hitautodestruct Oct 28 '12 at 11:36
  • 12
    according to @CMS `encodeURI` is not really safe for URL encoding. – Ifnot Mar 01 '13 at 16:35
  • 17
    @AnaelFavre because it is meant to encode the whole URL, which doesn't allow characters such as `:`, `/`, `@` etc. These 2 methods are not to be used interchangeable, you must know what you are encoding to use the right method. – Buu Mar 06 '13 at 19:32
  • 6
    See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI – Michał Perłakowski Jan 02 '16 at 18:15
  • As mentioned [in another answer on this page](http://stackoverflow.com/a/6171234/26510), [this site](http://stackoverflow.com/questions/75980/best-practice-escape-or-encodeuri-encodeuricomponent) nicely details the reason to use this method – Brad Parks May 06 '16 at 13:07
  • @BuuNguyen this does not work for me. I still see the my & as & in my terminal. – node_saini Oct 17 '16 at 14:31
  • @BuuNguyen : Hi BuuNguyen, thanks for useful solution. can please confirm me ? may I use encodeURIComponent() method for plain string encoding becuase in my application I am appending string content directly as a value to query string key then its leading XSS issue. its my code `var mywindow = window .open( "https://" + window.location.host + "center/js/cal.html?context=" + conString + "&date=" + dateString + "&firstName=" + firstName);` – Venkaiah Yepuri Dec 15 '16 at 09:23
  • @BuuNguyen : Now its saying `&firstName` could be cause the XSS issue. Now what I have to do ? if I do encode `firstname` then it will get resolved or not ? please suggest me something. Ty. – Venkaiah Yepuri Dec 15 '16 at 09:28
  • There are a lot of good sites out there that will let you play around with this: http://www.string-io.com is one. – germs12 Jul 20 '17 at 22:08
  • i had to pass a `+39123..` number with the plus symbol: the `encodeURI()` worked but my backend was not seeing it, while the `encodeURIComponent()` succeded (in angular 8 and asp.net core 3) – Biiz Jun 11 '20 at 11:29
  • 4
    NOTE: encodeURIComponent is only intended to be used on a URL's path. Query parameters follow an older percent-encoding specification which expects spaces to be encoded as "+" instead of "%20". See [this S.O. question](https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20) to learn more. Some servers may be lenient with this incorrect encoding, but your mileage may vary. In modern JavaScript, I would recommend encoding via URL or URLSearchParams as [this answer](https://stackoverflow.com/a/58879100/7696223) recommends. – Scotty Jamison May 14 '22 at 23:53
  • Don't forget the /g flag to replace all encoded ' ' – gurpartap Sep 29 '22 at 21:34
  • The PHP opposite of javascript encodeURIComponent() is rawurldecode(). In case anyone's interested. – Skyfish Jan 04 '23 at 15:07
1686

You have three options:

  • escape() will not encode: @*/+

  • encodeURI() will not encode: ~!@#$&*()=:/,;?+'

  • encodeURIComponent() will not encode: ~!*()'

But in your case, if you want to pass a URL into a GET parameter of other page, you should use escape or encodeURIComponent, but not encodeURI.

See Stack Overflow question Best practice: escape, or encodeURI / encodeURIComponent for further discussion.

user229044
  • 232,980
  • 40
  • 330
  • 338
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 86
    The character encoding used with escape is variable. Stick with encodeURI and encodeURIComponent, which use UTF-8. – erickson Dec 02 '08 at 04:55
  • 4
    I am using encodeURIComponent and noticing it will not encode pipe characters | – kevzettler Jan 30 '11 at 05:05
  • 15
    @kevzettler - why should it do that? The pipes aren't of semantic importance in a URI. – nickf Jan 31 '11 at 11:36
  • 8
    Be careful. That escape converts non-ASCII characters into its Unicode escape sequences, like `%uxxx`. – opteronn Mar 05 '10 at 20:10
  • 1
    does anybody use non-ASCII characters in URIs? – fiatjaf Jul 05 '13 at 21:45
  • 4
    @GiovanniP: People who allow German, French, Japanese, Chinese, Arabic characters as input and pass theses parameters via GET or POST. – Tseng Sep 04 '13 at 14:43
  • ah, ok, I thought you were talking about the domain/path parts, don't know why I thought this. – fiatjaf Sep 20 '13 at 23:18
  • 1
    `encodeURIComponent()` encoded the `#` and `encodeURI()` did not! – John N Oct 29 '14 at 19:30
  • 2
    @fiatjaf Non-ASCII characters are perfectly legitimate in domains as well, though there will be an ASCII version stored in the DNS system. http://en.wikipedia.org/wiki/Internationalized_domain_name – Vala Nov 03 '14 at 19:12
  • 3
    NOTE: as of JavaScript version 1.5 escape() has been deprecated. Stick with either encodeURI() or encodeComponent(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape – Dzeimsas Zvirblis Dec 16 '15 at 15:42
205

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-encode non-ASCII characters).

There is a nice explanation of the difference between encodeURI() and encodeURIComponent() elsewhere. If you want to encode something so that it can safely be included as a component of a URI (e.g. as a query string parameter), you want to use encodeURIComponent().

Community
  • 1
  • 1
Mike Brennan
  • 3,028
  • 2
  • 16
  • 7
98

The best answer is to use encodeURIComponent on values in the query string (and nowhere else).

However, I find that many older APIs want to replace " " with "+" so I've had to use the following:

const value = encodeURIComponent(value).replace('%20','+');
const url = 'http://example.com?lang=en&key=' + value

escape is implemented differently in different browsers and encodeURI doesn't encode many characters (like # and even /) -- it's made to be used on a full URI/URL without breaking it – which isn't super helpful or secure.

And as @Jochem points out below, you may want to use encodeURIComponent() on a (each) folder name, but for whatever reason these APIs don't seem to want + in folder names so plain old encodeURIComponent works great.

Example:

const escapedValue = encodeURIComponent(value).replace('%20','+');
const escapedFolder = encodeURIComponent('My Folder'); // no replace
const url = `http://example.com/${escapedFolder}/?myKey=${escapedValue}`;
Ryan Taylor
  • 12,559
  • 2
  • 39
  • 34
  • 26
    Please note, you should only replace %20 with + symbols after the first question mark (which is the 'query' part of the URL). Let's say I want to browse to `http://somedomain/this dir has spaces/info.php?a=this has also spaces`. It should be converted to: `http://somedomain/this%20dir%20has%spaces/info.php?a=this%20has%20also%20spaces` but many implementations allow '%20' in the querystring to be replaced by '+'. Nevertheless, you cannot replace '%20' with '+' in the path-section of the URL, this will result in a Not Found error unless you have a directory with a `+` instead of a space. – Jochem Kuijpers Jan 20 '13 at 01:08
  • @Jochem Kuijpers, definitely, you wouldn't put "+" in a directory. I'd only apply this to the query parameter values themselves (or keys if needed), not the entire URL, or even the entire query string. – Ryan Taylor Jul 19 '13 at 22:16
  • I would replace in value rather than in the result of the encoding – njzk2 Jun 02 '14 at 18:11
  • 1
    @njzk2 unfortunately ```encodeURIComponent('+')``` would give you ```%2B```, so you'd have to use two regular expressions... which I suppose is kinda why this works, because '+' are ' ' are encoded differently in the end. – Ryan Taylor Jun 02 '14 at 18:17
  • There is no reason to translate %20 to "+". The valid escape sequence for ASCII space is %20, not "+" which is not mentioned in RFC 3986 (https://tools.ietf.org/html/rfc3986). "+" was used in the 1990s; it is obsolete now and is only supported for legacy reasons. Don't use it. – xhienne Mar 26 '19 at 12:25
  • @xhienne for sure, it's just something i've noticed in the real world on a few APIs, it would always make more sense to use `encodeURIComponent` when that works – would be nice if people could stick to one standard! And preferably the RFC spec. – Ryan Taylor Mar 26 '19 at 15:20
  • Regardless if its a + or a %20, decoding the component should know how to interpret either value and produce a space. It is only specs such as oauth that require encoded strings within its spec where it makes a difference. – Gerard ONeill Apr 25 '19 at 20:23
43

I would suggest to use the qs npm package:

qs.stringify({a:"1=2", b:"Test 1"}); // gets a=1%3D2&b=Test+1

It is easier to use with a JavaScript object and it gives you the proper URL encoding for all parameters.

If you are using jQuery, I would go for the $.param method. It URL encodes an object, mapping fields to values, which is easier to read than calling an escape method on each value.

$.param({a:"1=2", b:"Test 1"}) // Gets a=1%3D2&b=Test+1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maksym Kozlenko
  • 10,273
  • 2
  • 66
  • 55
36

Modern solution (2021)

Since the other answers were written, the URLSearchParams API has been introduced. It can be used like this:

const queryParams = { param1: 'value1', param2: 'value2' }
const queryString = new URLSearchParams(queryParams).toString()
// 'param1=value1&param2=value2'

It also encodes non-URL characters.

For your specific example, you would use it like this:

const myUrl = "http://example.com/index.html?param=1&anotherParam=2";
const myOtherUrl = new URL("http://example.com/index.html");
myOtherUrl.search = new URLSearchParams({url: myUrl});
console.log(myOtherUrl.toString());

This solution is also mentioned here and here.

Qback
  • 4,310
  • 3
  • 25
  • 38
15

encodeURIComponent() is the way to go.

var myOtherUrl = "http://example.com/index.html?url=" + encodeURIComponent(myUrl);

But you should keep in mind that there are small differences from PHP version urlencode() and as @CMS mentioned, it will not encode every character. Guys at http://phpjs.org/functions/urlencode/ made JavaScript equivalent to phpencode():

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', '+');
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Fischer
  • 1,075
  • 11
  • 23
15

I think now in 2022 to be really safe, you should always consider constructing your URLs using the URL() interface. It'll do most of the job for you. So coming to your code,

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

Or...

const params = new URLSearchParams(myOtherUrl.search);

console.log(params.get('url'));
// Outputs: http://example.com/index.html?param=1&anotherParam=2

Something like this is assured not to fail.

m4heshd
  • 773
  • 9
  • 23
12

To encode a URL, as has been said before, you have two functions:

encodeURI()

and

encodeURIComponent()

The reason both exist is that the first preserves the URL with the risk of leaving too many things unescaped, while the second encodes everything needed.

With the first, you could copy the newly escaped URL into address bar (for example) and it would work. However your unescaped '&'s would interfere with field delimiters, the '='s would interfere with field names and values, and the '+'s would look like spaces. But for simple data when you want to preserve the URL nature of what you are escaping, this works.

The second is everything you need to do to make sure nothing in your string interfers with a URL. It leaves various unimportant characters unescaped so that the URL remains as human readable as possible without interference. A URL encoded this way will no longer work as a URL without unescaping it.

So if you can take the time, you always want to use encodeURIComponent() -- before adding on name/value pairs encode both the name and the value using this function before adding it to the query string.

I'm having a tough time coming up with reasons to use the encodeURI() -- I'll leave that to the smarter people.

Gerard ONeill
  • 3,914
  • 39
  • 25
10

What is URL encoding:

A URL should be encoded when there are special characters located inside the URL. For example:

console.log(encodeURIComponent('?notEncoded=&+'));

We can observe in this example that all characters except the string notEncoded are encoded with % signs. URL encoding is also known as percentage encoding because it escapes all special characters with a %. Then after this % sign every special character has a unique code

Why do we need URL encoding:

Certain characters have a special value in a URL string. For example, the ? character denotes the beginning of a query string. In order to successfully locate a resource on the web, it is necessary to distinguish between when a character is meant as a part of string or part of the URL structure.

How can we achieve URL encoding in JavaScript:

JavaScript offers a bunch of built-in utility functions which we can use to easily encode URLs. These are two convenient options:

  1. encodeURIComponent(): Takes a component of a URI as an argument and returns the encoded URI string.
  2. encodeURI(): Takes a URI as an argument and returns the encoded URI string.

Example and caveats:

Be aware of not passing in the whole URL (including scheme, e.g., https://) into encodeURIComponent(). This can actually transform it into a not functional URL. For example:

// 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"));

We can observe f we put the whole URL in encodeURIComponent that the forward slashes (/) are also converted to special characters. This will cause the URL to not function properly anymore.

Therefore (as the name implies) use:

  1. encodeURIComponent on a certain part of a URL which you want to encode.
  2. encodeURI on a whole URL which you want to encode.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
7

To prevent double encoding, it's a good idea to decode the URL before encoding (if you are dealing with user entered URLs for example, which might be already encoded).

Let’s say we have abc%20xyz 123 as input (one space is already encoded):

encodeURI("abc%20xyz 123")            //   Wrong: "abc%2520xyz%20123"
encodeURI(decodeURI("abc%20xyz 123")) // Correct: "abc%20xyz%20123"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
serg
  • 109,619
  • 77
  • 317
  • 330
6

A similar kind of thing I tried with normal JavaScript:

function fixedEncodeURIComponent(str){
    return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Narayan Yerrabachu
  • 1,714
  • 1
  • 19
  • 31
  • Can you explain your answer, please? E.g., why the magic hexadecimal number '2A'? (That is "\*" ([asterisk](https://en.wiktionary.org/wiki/asterisk#Noun)) in ASCII.) Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/16536783/edit), not here in comments (******************** ***without*** ******************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Dec 08 '22 at 00:39
6

You should not use encodeURIComponent() directly.

Take a look at RFC3986: Uniform Resource Identifier (URI): Generic Syntax

sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

The purpose of reserved characters is to provide a set of delimiting characters that are distinguishable from other data within a URI.

These reserved characters from the URI definition in RFC3986 ARE NOT escaped by encodeURIComponent().

MDN Web Docs: encodeURIComponent()

To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:

Use the MDN Web Docs function...

function fixedEncodeURIComponent(str) {
  return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
    return '%' + c.charCodeAt(0).toString(16);
  });
}
Community
  • 1
  • 1
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
4

Performance

Today (2020.06.12) I performed a speed test for chosen solutions on macOS v10.13.6 (High Sierra) on browsers Chrome 83.0, Safari 13.1, and Firefox 77.0. This results can be useful for massive URLs encoding.

Conclusions

  • encodeURI (B) seems to be fastest, but it is not recommended for URLs
  • escape (A) is a fast cross-browser solution
  • solution F recommended by MDN is medium fast
  • solution D is slowest

Enter image description here

Details

For solutions A B C D E F I perform two tests

  • for short URL - 50 characters - you can run it HERE
  • for long URL - 1M characters - you can run it HERE

function A(url) {
    return escape(url);
}

function B(url) {
    return encodeURI(url);
}

function C(url) {
    return encodeURIComponent(url);
}

function D(url) {
    return new URLSearchParams({url}).toString();
}

function E(url){
     return encodeURIComponent(url).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
}

function F(url) {
  return encodeURIComponent(url).replace(/[!'()*]/g, function(c) {
    return '%' + c.charCodeAt(0).toString(16);
  });
}



// ----------
// TEST
// ----------

var myUrl = "http://example.com/index.html?param=1&anotherParam=2";

[A,B,C,D,E,F]
  .forEach(f=> console.log(`${f.name} ?url=${f(myUrl).replace(/^url=/,'')}`));
This snippet only presents code of chosen solutions

Example results for Chrome

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
3

Nothing worked for me. All I was seeing was the HTML of the login page, coming back to the client side with code 200. (302 at first but the same Ajax request loading login page inside another Ajax request, which was supposed to be a redirect rather than loading plain text of the login page).

In the login controller, I added this line:

Response.Headers["land"] = "login";

And in the global Ajax handler, I did this:

$(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;
            }
        }
    });
});

Now I don't have any issue, and it works like a charm.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Asif Ashraf
  • 665
  • 1
  • 7
  • 15
3

Encode URL String

var url = $(location).attr('href'); // Get the 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 information, go to, jQuery Encode/Decode URL String.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sangeet Shah
  • 3,079
  • 2
  • 22
  • 25
3

Here is a live demo of encodeURIComponent() and decodeURIComponent() JavaScript built-in functions:

<!DOCTYPE html>
<html>
  <head>
    <style>
      textarea{
        width: 30%;
        height: 100px;
      }
    </style>
    <script>
      // 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()
      {
        var txt = document.getElementById("txt3").value;
        var result = atob(txt);
        document.getElementById("txt4").value = result;
      }
    </script>
  </head>
  <body>
    <div>
      <textarea id="txt1">Some text to decode
      </textarea>
    </div>
    <div>
      <input type="button" id="btnencode" value="Encode" onClick="encode()"/>
    </div>
    <div>
      <textarea id="txt2">
      </textarea>
    </div>
    <br/>
    <div>
      <textarea id="txt3">U29tZSB0ZXh0IHRvIGRlY29kZQ==
      </textarea>
    </div>
    <div>
      <input type="button" id="btndecode" value="Decode" onClick="decode()"/>
    </div>
    <div>
      <textarea id="txt4">
      </textarea>
    </div>
  </body>
</html>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
3

Use fixedEncodeURIComponent function to strictly comply with RFC 3986:

function fixedEncodeURIComponent(str) {
  return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
    return '%' + c.charCodeAt(0).toString(16);
  });
}
Community
  • 1
  • 1
Arthur
  • 3,056
  • 7
  • 31
  • 61
2

You can use ESAPI library and encode your URL using the below function. The function ensures that '/'s are not lost to encoding while the remainder of the text contents are encoded:

function encodeUrl(url)
{
    String arr[] = url.split("/");
    String encodedUrl = "";
    for(int i = 0; i<arr.length; i++)
    {
        encodedUrl = encodedUrl + ESAPI.encoder().encodeForHTML(ESAPI.encoder().encodeForURL(arr[i]));
        if(i<arr.length-1) encodedUrl = encodedUrl + "/";
    }
    return url;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohith Maratt
  • 171
  • 1
  • 5
1

Don't forget the /g flag to replace all encoded ' '

var myOtherUrl = "http://example.com/index.html?url=" + encodeURIComponent(myUrl).replace(/%20/g,'+');
gurpartap
  • 375
  • 2
  • 9
0

I always use this to encode stuff for URLs. This is completely safe because it will encode every single character even if it doesn't have to be encoded.

function urlEncode(text) {
    let encoded = '';
    for (let char of text) {
        encoded += '%' + char.charCodeAt(0).toString(16);
    }
    return encoded;
}
Pyzard
  • 451
  • 3
  • 14
  • even though this isn't the standard (wouldn't be used in a get request), its useful for encoding any character (e.g. for web exploitation), but would require padding with 0 for characters which are only 1 hex long. Also it should be uppercased. – TheSavageTeddy Nov 27 '22 at 02:31
0

let name = `bbb`;
params = `name=${name}`;
var myOtherUrl = `http://example.com/index.html?url=${encodeURIComponent(params)}`;
console.log(myOtherUrl); 

Use backtick now in ES6 to encode urls

try this - https://bbbootstrap.com/code/encode-url-javascript-26885283

Upasana Chauhan
  • 948
  • 1
  • 11
  • 32