Encode URL in JavaScript?

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?

Text Copied From stackoverflow.com
on : Tue, Dec 02, 2008 Views : 1260994

Accepted Answer

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);
 
Text Copied From stackoverflow.com
on : Tue, Dec 02, 2008

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp