Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Web Technology by (47.6k points)

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?

1 Answer

0 votes
by (106k points)
edited by

To check encode URL in JavaScript you can use the built-in function encode URIComponent(str) and encodeURI(str):-

Use the below-mentioned code:-

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

Want to become a Web Developer, join Web Development Courses now! 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 13, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer
+1 vote
1 answer

Browse Categories

...