简体   繁体   中英

encodeURIComponent and encodeURI

I am trying to encode the French characters inside the URL.

The url is constructed in javascript, and then I use window.open to open the url.

For example,

var alt_title='Thierry Frémaux: Les confiden'
  1. window.open('http://example.com/alt_title'=+encodeURIComponent(altCatTit));

  2. window.open(encodeURI('http://example.com/alt_title='+encodeURIComponent(altCatTit))

When 1. executes, I don't see the French characters being encoded in the new popup's url.

When 2. executes, I do see them being encoded.

Could you please tell me why do I need to encode them twice in order for them to appear as encoded in the new popup's URL?

Is it the right way of encoding special characters in the URL?

Fixed the typo's sorry!

are you looking for this?

var s = 'Thierry Frémaux: Les confiden'
var url = 'http://example.com/?alt_title=' + encodeURIComponent(s)

encodeURIComponent() : assumes that its argument is a portion (such as the protocol, hostname, path, or query string) of a URI. Therefore it escapes the punctuation characters that are used to separate the portionsof a URI.

encodeURI(): is used for encoding existing url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM