๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
D.evelop/JavaScript

[JavaScript] encodeURIComponent()๋ฅผ ์‚ฌ์šฉํ•œ URI ๊ณต์œ 

by Danne 2022. 7. 4.

 

๐Ÿ’ฅ'ํ…”๋ ˆ๊ทธ๋žจ ๋งํฌ ๊ณต์œ ํ•˜๊ธฐ' ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„์— ์žˆ์–ด '+'๋ฌธ์ž์—ด์ด ๋ฌด์‹œ๋˜๋Š” ๊ฒฝ์šฐ ๋ฐœ์ƒ.

https://t.me/+_abscefb1234

์œ„์™€๊ฐ™์€ ๋งํฌ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ–ˆ๋‹ค.

 

function shareTelegram() {
    let sendUrl = "https://t.me/_abscefb1234"; 
    window.open("https://telegram.me/share/url?url=" + "&url=" + sendUrl);
}

ํ•˜์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด + ๊ฐ€ ๋น ์ง„ ์ƒํƒœ๋กœ ์ „๋‹ฌ๋˜๋Š” ํ˜„์ƒ ๋ฐœ์ƒ

 

https://t.me/_abscefb1234

์ด๋ ‡๊ฒŒ ๊ณต์œ ๊ฐ€ ๋˜์—ˆ๋‹ค.

 

์ฃผ์†Œ์ฐฝ์—๋Š” ์›ํ•˜๋Š” url์ด ์ž˜ ์ „๋‹ฌ ๋˜์—ˆ๊ธฐ์— (+๋ชจ๋ฐ”์ผ์—์„œ๋Š” url ๋ฌธ์ž์—ด์ด ์™„๋ฒฝํžˆ ์ถœ๋ ฅ๋จ. PC์•ฑ์—์„œ๋งŒ ๋ฌธ์ œ๊ฐ€ ์ƒ๊น€._

ํ…”๋ ˆ๊ทธ๋žจ ์ž์ฒด์˜ ์–ด๋–ค ์ƒํ™ฉ์œผ๋กœ ์ธํ•ด ๋ฌธ์ž์—ด์— ๋ณ€ํ™”๊ฐ€ ์ƒ๊ธด๋‹ค๊ณ  ์ง์ž‘ํ–ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์ „๋‹ฌ ๋  ๋•Œ, ๋ณ€ํ™”์—†์ด ์ „๋‹ฌ ๋  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์‹œ๋„ํ–ˆ๋‹ค.

 

  • " " ๋”ฐ์˜ดํ‘œ, ` `๋ฐฑํ‹ฑ์œผ๋กœ ๋„ ๊ฐ์‹ธ๋ดค์ง€๋งŒ โŒ์‹คํŒจโŒ
  • ์ธ์ฝ”๋”ฉ์˜ ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ•ด, +์˜ ํŠน์ˆ˜๋ฌธ์ž ์ฝ”๋“œ์ธ &#43 ์œผ๋กœ ๋ณ€๊ฒฝํ•ด๋„ โŒ์‹คํŒจโŒ
  • JSON.stringify() ๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋” ์งฑ์งฑํ•˜๊ฒŒ ์ „๋‹ฌ ๋ ๊นŒ ์‹ถ์—ˆ๋Š”๋ฐ โŒ์‹คํŒจโŒ

 

๊ทธ๋Ÿฌ๋‹ค ์ฐพ์€ S.O. ์„ ์ƒ๋‹˜์˜ ๊ธ€์„ ์ฐพ์•˜๋‹ค. 

https://stackoverflow.com/questions/39012852/json-stringify-replace-pluses-with-spaces

 

JSON.stringify replace pluses with spaces

I use AJAX and send string someString to php-handler xmlHttp.open("POST", url, true); var someString = 'Foo+Bar'; var params = 'operation='+operation+'&json='+JSON.stringify(someString); xmlHttp.

stackoverflow.com

That has nothing to do with JSON.stringify.
The conversion of + to a is done by the URL parser on the server. A + is one of the ways you can represent a space in a URL (the other being %20).
To convert text to make it safe to insert into a URL, use encodeURIComponent.

- quentin

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” URLํŒŒ์„œ์— ์˜ํ•ด ๊ณต๋ฐฑ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ์ . 

์ข…์ข… ๋„์›Œ์“ฐ๊ธฐ(๊ณต๋ฐฑ)๋ฅผ ์‚ฌ์šฉํ•œ ํŒŒ์ผ๋ช…์ด๋‚˜ url์— + ๋˜๋Š” %20 ๋“ฑ์ด ๊ทธ ์ž๋ฆฌ๋ฅผ ์ฑ„์šฐ๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

ํ•ด๋‹น๋ฌธ์ œ๋Š” ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

encodeURIComponent(str)  : ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(str)์„ URI ๊ตฌ์„ฑ์š”์†Œ๋กœ์„œ ์ธ์ฝ”๋”ฉํ•ด ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

function shareTelegram() {
    let sendUrl = encodeURIComponent("https://t.me/_abscefb1234"); 
    window.open("https://telegram.me/share/url?url=" + "&url=" + sendUrl);
}

์„ฑ๊ณต!

 

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€