Language/JavaScript (WEB)

๐ŸŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ URL ๊ฐ์ฒด ์‚ฌ์šฉ๋ฒ•

์ธํŒŒ_ 2021. 12. 29. 11:09

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ URL๊ฐ์ฒด

URL ๊ฐ์ฒด

์ด์ „์—๋Š” window.location.href๋ฅผ ํ†ตํ•ด url๋ฌธ์ž์—ด์„ ์–ป์–ด์™€ ๋ฌธ์ž์—ด ํŒŒ์‹ฑ์„ ํ†ตํ•ด ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด์ œ url ์ •๋ณด๊ฐ’๋“ค์„ ๊ฐ์ฒด๋กœ ๋ฐ›์•„์™€ ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

๊ธฐ๋ณธ ์ œ๊ณต URL ํด๋ž˜์Šค๋Š” URL ์„ ๋งŒ๋“ค๊ณ  ๊ตฌ๋ฌธ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•œ ํŽธ๋ฆฌํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

// ์‚ฌ์ดํŠธ ์ฃผ์†Œ ์–ป๊ธฐ
const link = window.location.href;
// "http://testurl.co.kr:8080/path/main.html?param1=1&param2=3#top"

var url = new URL(link);
/* url ๊ฐ์ฒด ์ •๋ณด */
{
  protocol: "http:" // ํ”„๋กœํ† ์ฝœ
  hostname: "testurl.co.kr"  // ๋„๋ฉ”์ธ, ์•„์ดํ”ผ
  port: "8080" // ํฌํŠธ
  
  host: "testurl.co.kr:8080" // ์„œ๋ฒ„ ์ฃผ์†Œ
  origin: "http://testurl.co.kr:8080" // ํ”„๋กœํ† ์ฝœ, ์„œ๋ฒ„์ด๋ฆ„, ํฌํŠธ ํฌํ•จ
  href: "http://testurl.co.kr:8080/path/main.html?param1=1&param2=3#top" // ์ „์ฒด๊ฒฝ๋กœ
  
  pathname: "/path/main.html" // ์„œ๋ฒ„ ์ž์› ์œ„์น˜
  hash: "top" // #ํ•ด์‹œ ์œ„์น˜ 
   
  search: "?param1=1&param2=3" // ์ฟผ๋ฆฌ์ŠคํŠธ๋ง
  searchParams: URLSearchParams {} // ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๋ชจ๋“ˆํ™”
  
  username: "" // HTTP ์ธ์ฆ์ด์žˆ๋Š” ๊ฒฝ์šฐ ์†์„ฑ
  password: "" // HTTP ์ธ์ฆ์ด์žˆ๋Š” ๊ฒฝ์šฐ ์†์„ฑ
}

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธurl-๊ฐ์ฒด

์œ„์˜ ์ฝ”๋“œ๋Š” javascript๋กœ URL๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๊ฒƒ์ด๋‹ค. URL๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ƒ์„ฑ์ž์— ๊ธฐ๋ณธ์ ์ธ URL์„ ์ž…๋ ฅํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

'http://'๋˜๋Š” 'https://'๋ถ€ํ„ฐ url ํ˜•์‹์— ๋งž๊ฒŒ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค.


URL Parameter ๊ฐ’ ๋‹ค๋ฃจ๊ธฐ

๊ฐ’์„ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ getํ˜•์‹์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, search์†์„ฑ์œผ๋กœ url์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ถ€๋ถ„์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. searchParams ์†์„ฑ์€, ๋˜ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”๋ฐ ์ด๋ฅผ ์‚ฌ์šฉ๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค.

 

urlSearchParams.get()

: urlSearchParams์— .get('parameterName')์€ ํ•ด๋‹น parameterName์œผ๋กœ ์กฐํšŒ๋˜๋Š” ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ returnํ•œ๋‹ค.

 

urlSearchParams.getAll()

: urlSearchParams์— .getAll("parameterName")์€ parameterName์œผ๋กœ ์กฐํšŒ๋˜๋Š” ๋ชจ๋“  ๊ฐ’์„ ๋ฐฐ์—ด๋กœ returnํ•œ๋‹ค.

const urlStr = 'https://gillog/post/1?tag=javascript&like=backend';
const url = new URL(urlStr);

const urlParams = url.searchParams; // URLSearchParams ๊ฐ์ฒด๋ฅผ ๋„ฃ๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

const tag = urlParams.get('tag'); // ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์— tag๋ผ๋Š” ํ‚ค์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
console.log(tag); // javascript

const like = urlParams.get('like');
console.log(like) // backend

 

urlSearchParams.keys()

: parameter์— ํ•ด๋‹นํ•˜๋Š” key๋“ค์„ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” iterator๋ฅผ returnํ•œ๋‹ค.

 

urlSearchParams.values()

: parameter์— ํ•ด๋‹นํ•˜๋Š” value๋“ค์„ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” iterator๋ฅผ returnํ•œ๋‹ค.

 

urlSearchParams.entries()

: parameter์— ํ•ด๋‹นํ•˜๋Š” key์™€ value๋“ค์„ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” iterator๋ฅผ returnํ•œ๋‹ค.

// ์ฟผ๋ฆฌ์ŠคํŠธ๋ง ๋ถ€๋ถ„์„ URLSearchParams๊ฐ์ฒด์— ๋ฐ”๋กœ ์ค˜์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
const urlParams = new URLSearchParams("?gil=test&log=what&gillog=holy");

const keys = urlParams.keys();
// gil
// log
// gillog

const values = urlParams.values();
// test
// what
// holy

const entries = urlParams.entries();
// gil, test
// log, what
// gillog, holy

 

urlSearchParams.has()

: parameterName์— ํ•ด๋‹นํ•˜๋Š” Parameter๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

const urlParams = new URLSearchParams("?gil=123&log=456&gillog=777");

// true
console.log(urlParams.has("gillog"));

// false
console.log(urlParams.has("loggil"));

 

urlSearchParams.append()

: parameter๋ฅผ ์ถ”๊ฐ€

let urlParams = new URLSearchParams("?gil=test");

urlParams.append("log", "yes");
urlParams.append("log", "no");

// ?gil=test&log=yes&log=no
console.log(urlParams);

 

urlSearchParams.set()

: parameter์˜ value๊ฐ’์„ ๋ณ€๊ฒฝ. ๋งŒ์•ฝ ์กด์žฌํ•˜์ง€ ์•Š๋Š” parameterName์ด๋ผ๋ฉด append ๋œ๋‹ค.

let urlParams = new URLSearchParams("?gil=test&log=yes&log=no");

urlParams.set("gil", "yes");
urlParams.set("log", "wow");
urlParams.set("gillog", "good");

// ?gil=yes&log=wow&gillog=good
console.log(urlParams);

 

urlSearchParams.delete()

: parameter๋ฅผ ์ œ๊ฑฐ

url.searchParams.delete("param1")

# ์ฐธ๊ณ ์ž๋ฃŒ

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

https://javascript.info/url

https://velog.io/@gillog/javaScript-URL-Parameter-%EA%B0%92-%EB%8B%A4%EB%A3%A8%EA%B8%B0