본문 바로가기

[IT/Programming]/HTML related

URL | URI parser (URL 의 href, origin, protocol, host, hostname, port, pathname, search, hash 뽑아내기)

반응형
# URL | URI parser (URL 의 href, origin, protocol, host, hostname, port, pathname, search, hash 뽑아내기) URL | URI 에서 href, origin, protocol, host, hostname, port, pathname, search, hash 을 알아내고 싶을때가 있는데, javascript 단에서 제공하는 function 이 분명 존재할거라 생각해서 스스로 짜보다가 chatGPT 통해서 물어보고 알게 된 것 공유. ## PH
  • 2023-11-04 : First posting.
## TOC ## URL 의 기본구조 ```[.linenums] // The entire URL [href]=[protocol]//[hostname]:[port][/pathname][?search][#hash] =[protocol]//[host][/pathname][?search][#hash] =[origin][/pathname][?search][#hash] // Host [host]=[hostname]:[port] // Origin [origin]=[protocol]//[hostname]:[port] =[protocol]//[host] ```/ ## How to get href, origin, protocol, host, hostname, port, pathname, search, hash from URL? ```[.linenums.lang-js] m.analysisURL=function (url) { let parser=document.createElement('a'); parser.href=url; let res={}; res.href=parser.href; res.origin=parser.origin; res.protocol=parser.protocol; res.host=parser.host; res.hostname=parser.hostname; res.port=parser.port; res.pathname=parser.pathname; res.search=parser.search; res.hash=parser.hash; return res; }; ```/ ## RRA
  1. ChatGPT :: How can I get href, origin, protocol, host, hostname, port, pathname, search, hash from url?
  2. Change browser URL or URI (window.location, Unique Resource Identifier) without reloading or redirecting a page
반응형