2 Ways To Encode URL Using Javascript

In this article, I will tell about 2 ways of encoding URLs in Javascript. So let’s start.

Ways To Encode URL

There are two ways to encode URLs in Javascript.

  1. Using encodeURI()
  2. Using encodeURIComponent()

1. Using encodeURI()

This function is used to encode the full URL. The encodeURI() function encodes special characters except: ,/ ? : @ & = + $ #

The main purpose of this function is to encode the full URI. Check the below example.

var url = 'https://www.demoDomain.com?x=шеллы';
var encodedUrl = encodeURI(url);
console.log(encodedUrl);

2. Using encodeURIComponent()

The encodeURIComponent() function encodes the component of URL. It encodes special characters including the below characters.

,/ ? : @ & = + $ #

Check the below example.

var url = "https://www.demoDomain.com?x=шеллы&y=шеллы";
var encodeUrl = encodeURIComponent(url); 
console.log(encodeUrl);

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories