javascript url encode code example

Example 1: javascript base64 encode

var string = "Hello folks how are you doing today?";
var encodedString = btoa(string); // Base64 encode the String
var decodedString = atob(encodedString); // Base64 decode the String

Example 2: urlencode javascript

var url = encodeURIComponent('url');

Example 3: js escape url parameter

var myUrl = "http://www.image.com/?username=unknown&password=unknown";
var encodedURL= "http://www.foobar.com/foo?imageurl=" + encodeURIComponent(myUrl);

Example 4: js urlencode

/* There are 11 characters which are not encoded by encodeURI, 
but encoded by encodeURIComponent. */

encodeURI("test uri"); 
// "test%20uri"
encodeURI("test uri"); 
// "test%20uri"

encodeURI("test uri/with a slash"); 
// "test%20uri/with%20a%20slash"
encodeURIComponent("test uri/with a slash"); 
// "test%20uri%2Fwith%20a%20slash"

Example 5: type script encode url

var encodeURI = encodeURIComponent("&")

Example 6: swift url encode string

var urlString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)